nodelist

Why do nodelists contain extra undefined items that are not reflected in its length property?

夙愿已清 提交于 2019-12-01 09:38:43
问题 Background: I came across a very strange phenomenon while working with a node list. I wanted to use getElementsByClassName or something similar and then sort it. I decided one way would be to iterate through the nodelist and push each item to an array and sort the array. (This did work by the way but not as expected). I tried using the for (var i in nodeList) to iterate through, but it kept throwing an exception on the last few items, which were undefined. the weird part is I could instead

NodeList.prototype.forEach = Array.prototype.forEach;

强颜欢笑 提交于 2019-12-01 02:30:51
Do you see any problems with the following: NodeList.prototype.forEach = Array.prototype.forEach; Normally forEach is just a property of arrays, but by setting it as a property of all NodeList s as well, there's no need to convert a NodeList to an array before you can loop through its nodes with forEach . It's often not a good idea to extend the functionality of DOM through prototypes, especially in older versions of IE ( article ). However, you can simply use Array.prototype.forEach even without adding it to the prototype chain or converting your NodeList into an array: var list = document

NodeList.prototype.forEach = Array.prototype.forEach;

痴心易碎 提交于 2019-11-30 22:56:00
问题 Do you see any problems with the following: NodeList.prototype.forEach = Array.prototype.forEach; Normally forEach is just a property of arrays, but by setting it as a property of all NodeList s as well, there's no need to convert a NodeList to an array before you can loop through its nodes with forEach . 回答1: It's often not a good idea to extend the functionality of DOM through prototypes, especially in older versions of IE (article). However, you can simply use Array.prototype.forEach even

Speeding up xpath

戏子无情 提交于 2019-11-30 11:29:30
问题 i have a 1000 entry document whose format is something like <Example> <Entry> <n1></n1> <n2></n2> </Entry> <Entry> <n1></n1> <n2></n2> </Entry> <!--and so on--> There are more than 1000 Entry nodes here. I am writing a Java program which basically gets all the node one by one and do some analyzing on each node. But the problem is that the retrieval time of the nodes increases with its no. For example it takes 78 millisecond to retrieve the first node 100 ms to retrieve the second and it keeps

Speed of [].forEach.call(…?

血红的双手。 提交于 2019-11-30 09:51:31
I'm a big fan of using the forEach method on nodeLists like this: var nodes = document.querySelectorAll(".foo"); [].forEach.call(nodes, function (item) { //do stuff with item }); I was wondering though, does doing it that way take longer than the regular way? e.g. for(var i=0;i<nodes.length;i++){ //do stuff with nodes[i]; } Here's a nice performance comparison . According to it Array.forEach is slower than a native for loop. I know it's an old post but using the forEach method can be done by stealing the Array prototype as well. NodeList.prototype.forEach = Array.prototype.forEach; It depends

removing childNodes using node.childNodes.forEach

余生长醉 提交于 2019-11-30 09:44:27
问题 Traditionally, a suggested way of removing a node's children in Javascript is to do something like this: while(node.firstChild) { node.removeChild(node.firstChild); } Recently, I attempted to remove all of a node's children using the built in forEach() method: node.childNodes.forEach(child => { node.removeChild(child); } This didn't function as I expected. Instead of removing all child nodes, the forEach stopped executing, leaving nodes leftover. What I ended up having to do was use Array

Java XML: ClassCastException DeferredTextImpl

两盒软妹~` 提交于 2019-11-30 04:07:36
问题 Here is my code: // get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { // Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); // parse using builder to get DOM representation of the XML file dom = db.parse(file); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } NodeList n1 = dom

removing childNodes using node.childNodes.forEach

天涯浪子 提交于 2019-11-29 17:56:40
Traditionally, a suggested way of removing a node's children in Javascript is to do something like this: while(node.firstChild) { node.removeChild(node.firstChild); } Recently, I attempted to remove all of a node's children using the built in forEach() method: node.childNodes.forEach(child => { node.removeChild(child); } This didn't function as I expected. Instead of removing all child nodes, the forEach stopped executing, leaving nodes leftover. What I ended up having to do was use Array.from: Array.from(node.childNodes) And then I could remove nodes with forEach. The reason I could not use

Speed of [].forEach.call(…?

拥有回忆 提交于 2019-11-29 14:44:44
问题 I'm a big fan of using the forEach method on nodeLists like this: var nodes = document.querySelectorAll(".foo"); [].forEach.call(nodes, function (item) { //do stuff with item }); I was wondering though, does doing it that way take longer than the regular way? e.g. for(var i=0;i<nodes.length;i++){ //do stuff with nodes[i]; } 回答1: Here's a nice performance comparison. According to it Array.forEach is slower than a native for loop. 回答2: I know it's an old post but using the forEach method can be

Save new XML node to file

…衆ロ難τιáo~ 提交于 2019-11-29 08:47:23
I am trying to save nodeList node that contains XML as a new file, here is the Node list that get a new XML doc and split to smaller XMLs : public void split(Document inDocument) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); SaveXML savePerJob = new SaveXML(); // Load the input XML document, parse it and return an instance of the // Document class. Document document = inDocument; //all elements //jobs NodeList nodes = document.getDocumentElement()