简体   繁体   English

使用javascript连接html对象数组

[英]Concatenating html object arrays with javascript

I'm attempting to merge two arrays made up of html objects. 我正在尝试合并两个由html对象组成的数组。 For some reason using .concat() will not work for me. 出于某种原因,使用.concat()对我不起作用。

Here's a simple pen to demonstrate the problem: http://codepen.io/anon/pen/kIeyB 这是一个简单的笔来演示这个问题: http//codepen.io/anon/pen/kIeyB

Note: I tried searching for something remotely similar but found nothing that answered my question. 注意:我尝试搜索远程类似的东西但发现没有回答我的问题。

I figure you can do this the ole fashion way using for-loops but I rather not re-invent the wheel. 我认为你可以使用for循环来做这个方式,但我宁愿不重新发明轮子。

var x = document.getElementById("hello");
var items = x.getElementsByClassName("one");
//alert(items.length);
var items2 = x.getElementsByClassName("two");
//alert(items2.length);
items = items.concat(items2);
//alert(items.length);

items and items2 are nodeList or HTMLCollection objects, not arrays. itemsitems2nodeListHTMLCollection对象,而不是数组。 They do not contain a .concat() method. 它们不包含.concat()方法。 They have a .length property and support [x] indexing, but they do not have the other array methods. 它们具有.length属性和支持[x]索引,但它们没有其他数组方法。

A common workaround to copy them into an actual array is as follows: 将它们复制到实际数组中的常见解决方法如下:

// convert both to arrays so they have the full complement of Array methods
var array1 = Array.prototype.slice.call(x.getElementsByClassName("one"), 0);
var array2 = Array.prototype.slice.call(x.getElementsByClassName("two"), 0);

This can be also be done like this: 这也可以这样做:

var allitems = [];
allitems = Array.prototype.concat.apply(allitems, x.getElementsByClassName("one"));
allitems = Array.prototype.concat.apply(allitems, x.getElementsByClassName("two"));

The allitems variable will be a single javascript Array containing all elements with class one & two . allitems变量将是一个包含所有具有onetwo类的元素的javascript Array

document.getElementsByClassName doesn't return an array. document.getElementsByClassName不返回数组。 It returns NodeList which has length property. 它返回具有length属性的NodeList。

What you have are HTMLCollections, which although behave like arrays, but are not arrays. 你所拥有的是HTMLCollections,它虽然表现得像数组,但不是数组。 See here: https://developer.mozilla.org/en/docs/Web/API/HTMLCollection : 请参见: https//developer.mozilla.org/en/docs/Web/API/HTMLCollection

..A collection is an object that represents a lists of DOM nodes.. ..集合是一个表示DOM节点列表的对象。

In your case, you could concatenate these objects together into a new array: 在您的情况下,您可以将这些对象连接在一起成为一个新数组:

var itemsnew;
var x = document.getElementById("hello");
var items = x.getElementsByClassName("one");
var items2 = x.getElementsByClassName("two");
itemsnew = Array.prototype.concat.call(items, items2);

Now, if you: 现在,如果你:

console.log(itemsnew);

Will return: 将返回:

[HTMLCollection[1], HTMLCollection[1]]

And: 和:

console.log(itemsnew[0][0]);

Will return: 将返回:

<div class="one"></div>

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

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