简体   繁体   English

将循环变量作为元素函数中的参数传递

[英]Passing loop variable as argument in element function

Forgive the title, wasn't sure what to put. 原谅标题,不确定要放什么。

I have some code like: 我有一些类似的代码:

var links=document.getElementsByTagName('a');
for(var i=0;i<links.length;i++){
    var cur=links[i];
    cur.onmouseover=function(){alert(i);};
}

I remember seeing something like this before, but I have no clue how I would go about finding it. 我记得以前曾经见过类似的东西,但是我不知道如何去寻找它。 If another question like this has been asked, I would be far from surprised and would appreciate the link. 如果有人问过类似这样的问题,我不会感到惊讶,并且希望知道该链接。

edit: the problem is that it always alerts what 'i' is after the loop finishes. 编辑:问题是循环结束后,它始终会警告“ i”是什么。 If there are two links, they all alert 2. 如果有两个链接,它们都将发出警报2。

edit: I remembered seeing it here: http://nathansjslessons.appspot.com/ . 编辑:我记得在这里看到它: http : //nathansjslessons.appspot.com/ Great few lessons, I suggest anyone who hasn't already to do them. 很棒的几课,我建议任何尚未做过的人。

Try this: 尝试这个:

var links=document.getElementsByTagName('a'); 
for(var i=0;i<links.length;i++){     
    var cur=links[i];     
    cur.onmouseover=function(a){
        return function(){
            alert(a);
        }
    }(i); 
} 

You can actually use Array.forEach : 您实际上可以使用Array.forEach

var links=document.getElementsByTagName('a'); 
[].forEach.call(links, function(cur, i) {
    cur.onmouseover = function() { alert(i); };
})

The trick is using .call() , passing links as the this parameter. 诀窍是使用.call() ,并将links作为this参数传递。

You'll need to add .forEach() for older browsers that don't support it natively. 您需要为本身不支持它的旧版浏览器添加.forEach()

This is a more reusable solution. 这是一个更可重用的解决方案。 remember the 3 r's reduce reuse recycle ;) it overflows into real life. 记住3 r的减少重复使用回收;)它溢出到现实生活中。 see which is quickest optimization fun! 看看哪个是最快的优化乐趣! http://jsperf.com/speed-test-for-links-script http://jsperf.com/speed-test-for-links-script

var links       = document.getElementsByTagName('a'); 
var linksLength = document.getElementsByTagName('a').length //cache the length means quicker for loop

var addMouseOver = function(i){
    links[i].onmouseover=function(){
        alert(i);
    };
} //extract the function

for(var i=0;i<linksLength;i++){     
    addMouseOver(i);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将php变量作为函数参数传递 - passing php variable as function argument Ajax将php变量作为参数传递给javascript函数 - Ajax passing php variable to javascript function as an argument 将变量作为参数传递给函数并为其分配新值? - Passing a variable as an argument to a function and assigning a new value to it? 将数组作为函数参数传递给setTimeout的行为与传递变量不同 - passing an array as function argument to setTimeout acts not like passing a variable 将 function 作为参数传递给另一个 function 并将其应用于传入的元素 - Passing in a function as an argument to another function and apply it to the passed in element 通过作为参数和全局变量传递来访问函数内部的变量之间的区别? - Difference between accessing variable inside a function by passing as argument and global variable? 通过在函数中传递变量来选择元素-jQuery - Select element by passing variable in function - jQuery Javascript将变量传递给输入元素中的内联函数 - Javascript passing variable to inline function in input element 传递包含变量的HTML元素以替换函数 - Passing HTML element containing variable to replace function 在JavaScript中的for循环内将变量传递给内联函数 - Passing a variable to inline function within a for loop in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM