Concat two nodelists

前端 未结 4 1209
夕颜
夕颜 2021-02-04 02:17

I am trying to concat two nodelists using

var ul   = document.querySelector(\"ul\");
var down = document.getElementsByClassName(\"mobile\")[0];
var ul_child = Ar         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 02:26

    You can try converting the two NodeList objects to arrays first. Then calling concat on the results:

    // Convert the first list to an array
    var ul_list = document.querySelector("ul"),
        ul_children_array = Array.prototype.slice.call(ul_list.children);
    
    // Convert the second list to an array
    var down_list = document.getElementsByClassName("mobile")[0],
        down_children_array = Array.prototype.slice.call(down_list.children);
    
    var ul_child_array = Array.prototype.concat.call(ul_children_array, down_children_array);
    

提交回复
热议问题