简体   繁体   English

像javascript中的对象一样在数组上应用forEach方法

[英]apply forEach method on array like objects in javascript

Here i have four input buttons and i want to add event handler for each of them.If i use for loop i have to deal with closures.Actually i am trying to understand how to manage forEach method for array like object.This is the closest solution i can manage.As we have to encounter closure related problem if we want to use array methods.I had to use object.keys to get the indexes and applied forEach on them.Though it sounds strange but i am not quite satisfied with this solution.Can it be more simpler?How can i manage forEach directly on the nodeList stored in Buttons variable这里我有四个输入按钮,我想为每个按钮添加事件处理程序。如果我使用 for 循环,我必须处理闭包。实际上我想了解如何管理类似对象的数组的 forEach 方法。这是最接近的我可以管理的解决方案。因为如果我们想使用数组方法,我们必须遇到与闭包相关的问题。我不得不使用 object.keys 来获取索引并在它们上应用 forEach。虽然这听起来很奇怪,但我对此不太满意解决方案。可以更简单吗?如何直接在存储在 Buttons 变量中的 nodeList 上管理 forEach

 function change(){ var buttons=document.getElementsByTagName('input'); var keys=Object.keys(buttons); keys.forEach(function(el,indx,arr){ if(el != 'length'){ this[el].addEventListener('click', function(e){ alert(e.target.value); }); } }, buttons); } change();
 <input type='button' value='button1'> <input type='button' value='button2'> <input type='button' value='button3'> <input type='button' value='button4'>

document.getElementsByTagName returns you a "host object". document.getElementsByTagName返回一个“宿主对象”。 These objects can differ in behavior between browsers.这些对象在浏览器之间的行为可能不同。

buttons is aNodeList , not an array. buttons是一个NodeList ,而不是一个数组。 Object.keys may not work as expected here because it might have more properties than just the indexes and length . Object.keys在这里可能无法按预期工作,因为它可能具有比索引和length更多的属性。

The docs for NodeList ( https://developer.mozilla.org/en-US/docs/Web/API/NodeList ) have some examples to convert it into an array and how to use it with .forEach . NodeList ( https://developer.mozilla.org/en-US/docs/Web/API/NodeList ) 的文档有一些示例可以将其转换为数组以及如何将其与.forEach一起使用。

The method I normally use isn't listed on the docs page.我通常使用的方法没有列在文档页面上。 I usually use:我通常使用:

var arr = Array.prototype.slice.call(buttons);

See this question for more info: Fastest way to convert JavaScript NodeList to Array?有关更多信息,请参阅此问题:将 JavaScript NodeList 转换为数组的最快方法?

Then you can do:然后你可以这样做:

arr.forEach(function(el){
    el.addEventListener('click', function(e){ 
        alert(e.target.value);
    });
});

Here's a demo:这是一个演示:

 var buttons = document.getElementsByTagName('input'); var arr = Array.prototype.slice.call(buttons); arr.forEach(function(el) { el.addEventListener('click', function(e) { alert(e.target.value); }); });
 <input type='button' value='button1'> <input type='button' value='button2'> <input type='button' value='button3'> <input type='button' value='button4'>

ES6 Solution ES6 解决方案

The ES6 Array.from method can convert array-like object to an array : ES6 Array.from方法可以将类数组对象转换为数组:

 var buttons = document.getElementsByTagName('input'); Array.from(buttons).forEach(el=>{ el.addEventListener('click', e =>{ alert(e.target.value); }); });
 <input type='button' value='button1'> <input type='button' value='button2'> <input type='button' value='button3'> <input type='button' value='button4'>

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

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