html中有16个框。
<div id="box-container">
<div class="box" data-coord="0:0" style="clear: left"></div> <div class="box" data-coord="0:1"></div> <div class="box" data-coord="0:2"></div> <div class="box" data-coord="0:3"></div>
<div class="box" data-coord="1:0" style="clear: left"></div> <div class="box" data-coord="1:1"></div> <div class="box" data-coord="1:2"></div> <div class="box" data-coord="1:3"></div>
<div class="box" data-coord="2:0" style="clear: left"></div> <div class="box" data-coord="2:1"></div> <div class="box" data-coord="2:2"></div> <div class="box" data-coord="2:3"></div>
<div class="box" data-coord="3:0" style="clear: left"></div> <div class="box" data-coord="3:1"></div> <div class="box" data-coord="3:2"></div> <div class="box" data-coord="3:3"></div>
</div>
我想编写将执行以下操作的js代码:
当我点击任何一个方框时,它会提示它的订单号从1到16
我的js代码如下:
<script type="text/javascript">
var boxArray = document.getElementsByClassName("box");
for (var i = 0; i < boxArray.length; i++) {
boxArray[i].onclick = function () {
var say = function (i) {
alert(i);
};
say(i);
}
}
</script>
但每次提醒 16
有什么问题?
你能帮帮我吗?
答案 0 :(得分:4)
使用经典IIFE: Immediately Invoked Function Expression,你可以写:
var boxArray = document.getElementsByClassName("box");
for (var i = 0; i < boxArray.length; i++) {
boxArray[i].onclick = (function (i) {
return function (e) {
var say = function (i) {
alert(i);
};
say(i);
};
})(i);
}
<div id="box-container">
<div class="box" data-coord="0:0" style="clear: left">1</div> <div class="box" data-coord="0:1">2</div> <div class="box" data-coord="0:2">3</div> <div class="box" data-coord="0:3">4</div>
<div class="box" data-coord="1:0" style="clear: left">5</div> <div class="box" data-coord="1:1">6</div> <div class="box" data-coord="1:2">7</div> <div class="box" data-coord="1:3">8</div>
<div class="box" data-coord="2:0" style="clear: left">9</div> <div class="box" data-coord="2:1">10</div> <div class="box" data-coord="2:2">11</div> <div class="box" data-coord="2:3">12</div>
<div class="box" data-coord="3:0" style="clear: left">13</div> <div class="box" data-coord="3:1">14</div> <div class="box" data-coord="3:2">15</div> <div class="box" data-coord="3:3">16</div>
</div>
答案 1 :(得分:3)
这与javascript的范围有关。 i
超出了for循环的范围,因为您使用了var
,所以当您执行alert(i)
时,它会获取循环后留下i
的值(因为循环已经运行了。)
从
更改for循环for (var i = 0; i < boxArray.length; i++)
到
for (let i = 0; i < boxArray.length; i++)
它应该解决你的问题。
答案 2 :(得分:1)
您可以将addEventListener
与本地声明的变量一起使用,例如
var boxArray = document.getElementsByClassName("box");
for (var i = 0; i < boxArray.length; i++) {
boxArray[i].addEventListener('click', (function(e) {
var num = i;
return function() {
alert(num);
}
})());
}
<div id="box-container">
<div class="box" data-coord="0:0" style="clear: left">1</div>
<div class="box" data-coord="0:1">2</div>
<div class="box" data-coord="0:2">3</div>
<div class="box" data-coord="0:3">4</div>
<div class="box" data-coord="1:0" style="clear: left">5</div>
<div class="box" data-coord="1:1">6</div>
<div class="box" data-coord="1:2">7</div>
<div class="box" data-coord="1:3">8</div>
</div>
答案 3 :(得分:0)
如果你使用jQuery&#39; s .index()
这就像这样简单:
$(".box").click(function(){
var i = $(this).index();
alert(i);
});
DEMO:http://jsbin.com/zoxirafufo/edit?html,js,console,output