为clickTags实现var名称的循环

时间:2016-12-06 23:07:17

标签: javascript loops clicktag

需要为横幅添加三个clickTag,其名称类似于clickTag1,clickTag2,clickTag3。现在代码看起来像这样:

for(var i = 1; i <= 3; i++) {
    document.getElementById('Destination_cta_' + i).addEventListener('click', function() {
        window.open(clickTag2, '_blank'); //here I want clickTag look like clickTag + i, but its not working.
    })
  }

所以问题是如何循环var名称,所以我不需要像现在一样手动放置它。

3 个答案:

答案 0 :(得分:1)

您想为此使用数组。数组是一个索引的值列表。

var clickTags = ["","www.nba.com","www.nhl.com","www.nfl.com"];
for(var i = 1; i <= 3; i++) {
    document.getElementById('Destination_cta_' + i).addEventListener('click', function() {
        window.open(clickTags[i], '_blank'); //here I want clickTag look like clickTag + i, but its not working.
    })
}

注意,因为您在1而不是0开始循环,所以我为clickTags数组的索引0添加了一个空白条目。

答案 1 :(得分:1)

解决此问题的最简单方法是使用Array

[, clickTag1, clickTag2, clickTag3].forEach(function(e, i) {
    document.getElementById('Destination_cta_' + i).addEventListener('click', function() {
        window.open(e, '_blank');
    })
})

另一种方法:如果clickTag是全局变量,则可以始终将它们作为window对象的全局属性进行访问:

for(var i = 1; i <= 3; i++) (function (i) {
    document.getElementById('Destination_cta_' + i).addEventListener('click', function() {
        window.open(window['clickTag' + i], '_blank')
    })
)(i)

附加包装功能修复了上面评论中提到的闭包错误。

答案 2 :(得分:1)

为什么它目前没有按您的意图运作:

&#13;
&#13;
for (var i = 1; i <= 3; i++) {
  // During your first loop there is a local variable `i` whose value is 1
  document.getElementById('Destination_cta_' + i)
    // Here you pass an anonymous function as the second argument to addEventListener
    // This creates a closure, which means the function's context includes variables
    // that were in scope when it was created. Right now we have the `for` loop's variable
    // `i` in the current scope, so the function keeps a *reference* to that variable.
    .addEventListener('click', function() {
     
      // When this get executed in the future, the function has to know about the variable `i`,
      // and thankfully there is a reference to it in this function's closure. But remember that
      // the for loop executed 3 times, using that same variable. That means that every function
      // was created with a closure that is keeping a reference to the same variable, whose final 
      // value after the loop finished, was 4. 
      window.alert('clickTag' + i);  // Will always alert 'clickTag4' no matter which is clicked
  })
}
&#13;
<div id="Destination_cta_1">1</div>
<div id="Destination_cta_2">2</div>
<div id="Destination_cta_3">3</div>
&#13;
&#13;
&#13;

如何解决这个问题?

确保每个addEventListener调用在其自己的闭包中获得具有正确值的函数。这样做的方法是使用一个立即调用的函数表达式,传递给你想要的值:

&#13;
&#13;
for (var i = 1; i <= 3; i++) {
  var element = document.getElementById('Destination_cta_' + i)

  element.addEventListener('click', (function(index) {
    // This function is now a closure with a reference to index
    return function() { 
      window.alert('clickTag' + index);
    }
  })(i)) // calling the anonymous function with the current value of `i` binds that value
         // to the function's scope
}
&#13;
<div id="Destination_cta_1">1</div>
<div id="Destination_cta_2">2</div>
<div id="Destination_cta_3">3</div>
&#13;
&#13;
&#13;