Can we Directly remove Nodes from a NodeList?

后端 未结 3 547
无人及你
无人及你 2020-12-31 01:01

document.getElementsByTagName returned me a NodeList object.

I would like to remove some items (let\'s say I would like to remove the first item from th

3条回答
  •  时光说笑
    2020-12-31 01:37

    var MySet = { 0: {value: document.body, enumerable: true}, 1: {value: document.head, enumerable: true}, 2:{value: document.documentElement, enumerable: true}, length: {value: 3}};
    var protoNodelist = document.createDocumentFragment().childNodes;
    var MyNodelist = Object.create(protoNodelist, MySet);
    MyNodelist; // Output: NodeList {0: body, 1: head, 2: html, length: 3}
    removeNode(MyNodelist, 1); // Output: NodeList {0: body, 1: html, length: 2}
        function removeNode(NodeList, i){
            var newElementSet = {};
            var newElementSeti = 0;
            for(NodeListi = 0; NodeListi < NodeList.length; NodeListi++){
                if(NodeListi != i){
                    newElementSet[newElementSeti] = {value: NodeList[NodeListi], enumerable: true};
                    newElementSeti += 1;
                };
            };
            newElementSet['length'] = {value: newElementSeti};
            var nodelist = document.createDocumentFragment().childNodes;
            var newNodelist = Object.create(nodelist, newElementSet);
            newNodelist.__proto__ = newNodelist.__proto__.__proto__;
            return newNodelist;
        };
    

提交回复
热议问题