简体   繁体   English

具有多个参数的JavaScript addEventListener问题

[英]JavaScript addEventListener problems with multiple arguments

This is the prompt: In the event listener DOMContentLoaded set up two addEventListener methods for each of the second input elements (using the array of inputElements[] and the corresponding index) and passing the first and the second value (use inputElements[].value ) and the appropriate span element (using spanElements[] ). 这是提示:在事件侦听器DOMContentLoaded中,为每个第二个输入元素设置两个addEventListener方法(使用inputElements[]数组和相应的索引),并传递第一个和第二个值(使用inputElements[].value )和适当的span元素(使用spanElements[] )。 Since you need to pass arguments, use the following construct: 由于您需要传递参数,因此请使用以下构造:

inputElements[index].addEventListener('blur',function(){ fCompareInput(arguments); })

This is the Function: function fCompareinput(value1,value2,display) 这是函数:函数fCompareinput(value1,value2,display)

This is what I have: 这就是我所拥有的:

inputElements[2].addEventListener('blur', function() {
    fCompareInput(inputElements[2].value);
});
inputElements[2].addEventListener('blur', function() {
    fCompareInput(spanElements[2].value);
});

inputElements[4].addEventListener('blur', function() {
    fCompareinput(inputElements[4].value);
});
inputElements[4].addEventListener('blur', function() {
    fCompareinput(spanElements[4].value);
});

I don't think I'm passing the arguments correctly. 我认为我没有正确传递参数。

It's simply easier to go through your inputElements collection using a loop, and then bind the event listener. 使用循环inputElements集合,然后绑定事件侦听器,将更加容易。 Also: 也:

  1. You can place multiple function calls in the blur callback 您可以在blur回调中放置多个函数调用
  2. Use this in the callback to refer to the DOM node that has triggered the event 在回调中使用this来引用已触发事件的DOM节点

Let's say you only want the element with the index of 2 and 4 to have such events bound, then we can do this: 假设您只希望索引为24的元素绑定此类事件,那么我们可以这样做:

// Define which indexes you want to bind eventistener to
var indexes = [2,4];

// Loop through the entire collection
for (let i = 0; i < inputElements.length; i++) {
    if (indexes.indexOf(i) !== -1) {
        inputElements[i].addEventListener('blur', function() {
            fCompareinput(this.value);
            fCompareinput(spanElements[i].value);
        });
    }
}

Supplementary: 补充:

  • If you want to retrieve all even numbered element (by index) in the collection, use: 如果要检索集合中所有偶数元素(按索引),请使用:

     if (i % 2 === 0) 
  • If you want to retrieve all even numbered elements and excluding the first element, use: 如果要检索所有偶数编号的元素排除第一个元素,请使用:

     if (i % 2 === 0 && i !== 0) 

For example: 例如:

// Loop through the entire collection
for (let i = 0; i < inputElements.length; i++) {
    if (i % 2 === 0) {
        inputElements[i].addEventListener('blur', function() {
            fCompareinput(this.value);
            fCompareinput(spanElements[i].value);
        });
    }
}

Since you have tagged your question with jQuery , there is also a jQuery-based solution: much simpler, but of course incurs an overhead of having to load a library. 由于您已经用jQuery标记了问题,所以还有一个基于jQuery的解决方案:简单得多,但是当然会导致必须加载库的开销。

var indexes = [2,4];

$(inputElements).each(function(i) {
    if (indexes.indexOf(i) !== -1) {
        $(this).on('blur', function() {
            fCompareinput(this.value);
            fCompareinput($(spanElements).get(i).val());
        });
    }
});

...or, if you want to select even numbered indexes only: ...或者,如果您只想选择偶数索引:

$(inputElements).each(function(i) {
    if (i % 2 === 0) {
        $(this).on('blur', function() {
            fCompareinput(this.value);
            fCompareinput($(spanElements).get(i).val());
        });
    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM