javascript - for 循环中的引用数组生成悬停事件

标签 javascript jquery for-loop

如何引用与 for 循环中使用的数组具有相同索引的数组项?

animations[i] 当前返回未定义:

var icons = ["#know","#run","#think","#done","#measure","#plan","#resolve"]
var animations = [knowTl,runTl,thinkTl,doneTl,measureTL,planTl,resolveTl]

for(var i=0;i<icons.length;i++) {
$(icons[i]).hover(
  function() {
  animations[i].restart()
  },
  function() {}
 );
}

最佳答案

这样做

for(var i=0;i<icons.length;i++) {
(function(i){
  $(icons[i]).hover(
    function() {
      animations[i].restart()
    },
    function() {}
   );
})(i)
}

关于javascript - for 循环中的引用数组生成悬停事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42695727/

相关文章:

jQuery for 循环动态值

javascript - 如何将 URL 参数添加到给定 href 中数据属性的 URL?

javascript - ui-router状态url问题

jquery - 如何使用 Selectize.js 以编程方式使特定选项不可选择?

javascript - ASP Net MVC 从 JS 错误调用 Controller 中的函数

java - 将字符打印为矩阵

Javascript滚动视差覆盖页面上的其他元素

javascript - 无法将边框应用于 HTML 表格

javascript - 生成 html 时,将 .push() 填充的数组存储在 html 数据属性中

node.js - Node js等待函数返回以继续for循环