[英]Recall javascript array index value
I'm implementing a way to track clicks in a tabbed content section. 我正在实现一种跟踪选项卡式内容部分中点击的方法。
When I click any tab, the parameter is passing as clicked=tab4. 当我单击任何选项卡时,该参数将作为clicked = tab4传递。 It should be clicked=tab(value of 0-3).
应该单击== tab(值为0-3)。 It looks like it's returning the length of the array?
看起来它正在返回数组的长度? How can I make the event listener function keep the index value of the array.
如何使事件侦听器函数保留数组的索引值。
Example: when I click tab1, the variable "i" in the event listener should be 0. 示例:当我单击tab1时,事件侦听器中的变量“ i”应为0。
Here is my code. 这是我的代码。 It works except for this one part, and I can't figure out why.
除这一部分外,它都有效,我不知道为什么。
function clickTrack(){
var tabs = ['tab1', 'tab2', 'tab3', 'tab4'];
for(i = 0; i < tabs.length; i++) {
document.getElementById(tabs[i]).addEventListener("click", function(){
trackingFunction('param1','clicked=tab'+ i);
alert(i);
});
}
}
You have to define an new execution context in order to save the state of the index variable like: 您必须定义一个新的执行上下文以保存索引变量的状态,例如:
function clickTrack(){
var tabs = ['tab1', 'tab2', 'tab3', 'tab4'];
for(i = 0; i < tabs.length; i++) {
(function(index){
document.getElementById(tabs[i]).addEventListener("click", function(){
trackingFunction('param1','clicked=tab'+ index);
alert(i);
});
})(i);
}
}
An Immediately-Invoked Function Expression can be used to “lock in” values and effectively save state of the index variable. 立即调用的函数表达式可用于“锁定”值并有效保存索引变量的状态。
This would be a lot cleaner with forEach()
: 使用
forEach()
会更干净:
function clickTrack(){
['tab1', 'tab2', 'tab3', 'tab4'].forEach(function(tabId) {
document.getElementById(tabId).addEventListener("click", function(){
trackingFunction('param1','clicked=' + tabId);
alert(tabId);
});
});
}
Note: Array#forEach
is IE9+, so you would need to polyfill it for older browsers. 注意:
Array#forEach
是IE9 +,因此对于较旧的浏览器,您需要对其进行填充。
Regarding the why part of your question, let's look at a relevant subset of your code: 关于问题的部分原因 ,让我们看一下代码的相关子集:
for(i = 0; i < tabs.length; i++) {
document.getElementById(tabs[i]).addEventListener("click", function(){
alert(i);
});
}
The thing to note here is the closure that is created by click event handler. 这里要注意的是单击事件处理程序创建的闭包。 Specifically, it retains a reference to the
i
variable, which continues to be incremented until the i < tabs.length
condition fails. 具体来说,它保留对
i
变量的引用,该引用将继续增加,直到i < tabs.length
条件失败为止。 All those increments happen synchronously, so by the time the click handler ever runs, it's guaranteed that the value of i
will be tabs.length
. 所有这些增量都是同步发生的,因此在单击处理程序运行时,可以确保
i
的值将为tabs.length
。
If you compare that with the forEach()
example, you'll notice that the variable being enclosed by the click handler is tabId
, which is a unique, new variable for each iteration, and therefore doesn't suffer from the same issues as the for
loop. 如果将其与
forEach()
示例进行比较,您会注意到单击处理程序包含的变量是tabId
,这是每次迭代的唯一新变量,因此不会遇到与for
循环。
Replace "i" for its id... 将“ i”替换为其ID ...
http://jsfiddle.net/yzu7kLf4/ http://jsfiddle.net/yzu7kLf4/
Try this: 尝试这个:
function clickTrack(){
var tabs = ['tab1', 'tab2', 'tab3', 'tab4'];
for(i = 0; i < tabs.length; i++) {
document.getElementById(tabs[i]).addEventListener("click", function(){
trackingFunction('param1','clicked='+ this.id);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.