简体   繁体   English

如何使用JavaScript连接HTML元素的两个数组

[英]How to join two arrays of html elements with JavaScript

I`v already tried concat and push methods but it did not worked to me. 我已经尝试了concat和push方法,但对我没有用。 Thats what i want to do : 那就是我想做的事:

var left = document.getElementById('main-left').children;
var right = document.getElementById('main-right').children;
var main = left.concat(right); 

left = [<article class=​"content">​…​</article>​, <article class=​"content">​…​     </article>​, <article class=​"content">​…​</article>​]
right = [<article class=​"content">​…​</article>​, <article class=​"content">​…​ </article>​, <article class=​"content">​…​</article>​]

getElementById() never returns an array, so push and concat won't be available on 'left' and 'right' objects. getElementById()永远不会返回数组,因此push和concat在'left'和'right'对象上将不可用。 you can try something like this: 您可以尝试这样的事情:

    function logElementArray(){
    var leftArray = document.getElementById('left').children;
    var rightArray = document.getElementById('right').children;
    var mainArray=[];
    for(var i=0; i<rightArray.length; i++){
        mainArray.push(rightArray[i]);
    }
    for(var i=0; i<leftArray.length; i++){
        mainArray.push(leftArray[i]);
    }
 }

Your 'mainArray' will have all the elements then. 然后,您的“ mainArray”将具有所有元素。

You indeed should use concat, but like this: 您确实应该使用concat,但是像这样:

var left = document.getElementById('main-left').children;
var right = document.getElementById('main-right').children;

left = [].slice.call(left);
var main = left.concat.apply(left, right);

// or even var main = [].concat.apply([], [[].slice.call(left), [].slice.call(right)]);

left is not an Array instance, so it doesn't have Array.prototype methods like slice or concat , so you need to use call . left不是Array实例,因此它没有诸如sliceconcat类的Array.prototype方法,因此您需要使用call

Or you could of course simple use for loop and populate new array in loop. 或者,您当然可以简单地用于循环并在循环中填充新数组。

Here is a code snippet for adding elements to an array and then adding the elements in the array to an element. 这是一个代码片段,用于将元素添加到数组,然后将数组中的元素添加到元素。

If this is what your trying to do using appendChild() is probably better. 如果这是您尝试使用appendChild()可能更好的方法。

 var a = document.getElementsByClassName("test"); var b = ""; for (var i = 0; i < a.length; i++) { b += a[i].outerHTML; console.log(a[i]); } var c = document.getElementById("main"); c.innerHTML = b; 
 .test { border: 5px solid cornflowerblue; height: 50px; } #main { border: 5px solid firebrick; } 
 <div class="test"></div> <div class="test"></div> <div id="main"></div> 

First see that left and right are not arrays in any sense. 首先看到leftright都没有在任何意义上的阵列。 You have to perform document.querySelectorAll or document.getElementsByClassName to get an array and then you can apply array like methods to join them. 您必须执行document.querySelectorAlldocument.getElementsByClassName才能获取数组,然后可以应用数组之类的方法来将它们联接。 Most preferably use: 最优选使用:

// `a` into `b`:
for (var i=a.length-1; i >= 0; i--) {
     b.unshift( a[i] );
}

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

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