[英]Javascript Joining 2 Arrays
How to join 2 Arrays containing DOM Elements. 如何加入2个包含DOM元素的数组。
I am trying to concat 2 or more array that contain Selectbox elements. 我试图连接2个或更多包含Selectbox元素的数组。 Here is my sample code.
这是我的示例代码。
selectboxes = [];
for(var i = 0; i < forms.length; i++)
{
selectboxes = selectboxes.concat(forms[i].getElementsByTagName('select'))
}
but it's not working. 但它不起作用。 Any tip?
有提示吗?
JavaScript's getElementsByTagName function does not return an array it returns a NodeList which does not have a concat method. JavaScript的getElementsByTagName函数不返回一个数组,它返回一个没有concat方法的NodeList。
https://developer.mozilla.org/en/DOM/element.getElementsByTagName https://developer.mozilla.org/en/DOM/element.getElementsByTagName
See this answer: JavaScript NodeList 看到这个答案: JavaScript NodeList
For an example of how to put them in one array. 有关如何将它们放在一个数组中的示例。
I think the best solution would be this: 我认为最好的解决方案是:
http://jsfiddle.net/4CKRN/ http://jsfiddle.net/4CKRN/
-- UPDATE for your specific code -- - 更新您的特定代码 -
forms = document.forms;
selectboxes = [];
for(var i = 0; i < forms.length; i++)
{
selectboxes = selectboxes.concat(Array.prototype.slice.call(forms[i].getElementsByTagName('select')));
}
Keep in mind if you just wanted to get all the selectboxes you could skip selecting them from within the forms and just select them from document.getElementsByTagName("select") 请记住,如果您只想获取所有选择框,可以跳过从表单中选择它们,只需从document.getElementsByTagName(“select”)中选择它们即可
Of course, then you may get select boxes that weren't meant for forms though (occasionally select boxes are used for navigation or other non-standard reasons). 当然,那么你可能会得到不适用于表格的选择框(偶尔选择框用于导航或其他非标准原因)。
I have created a new function for array and it is working fine. 我已经为数组创建了一个新函数,它工作正常。
Array.prototype.merge = function(arr)
{
var len = this.length;
for(var idx = 0; idx < arr.length; idx++)
{
this[len+idx] = arr[idx]
}
return this;
}
calling like 打电话给
result = arr1.merg(arr2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.