Created
June 9, 2014 01:33
-
-
Save Williammer/85c9e1d70093039d58f9 to your computer and use it in GitHub Desktop.
jsPerform.domPract.js - several performance efficient ways to manipulate dom.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //use ECMA5 selector api. | |
| document.querySelector("ul .selected"); | |
| document.querySelectorAll("#widget .class"); | |
| //use documentFragment to cache dom elements | |
| var p, t, frag; | |
| frag = document.createDocumentFragment(); | |
| p = document.createElement('p'); | |
| t = document.createTextNode('first paragraph'); | |
| p.appendChild(t); | |
| frag.appendChild(p); | |
| p = document.createElement('p'); | |
| t = document.createTextNode('second paragraph'); | |
| p.appendChild(t); | |
| frag.appendChild(p); | |
| document.body.appendChild(frag); | |
| //use cloneNode to copy node and change on it. | |
| var oldnode = document.getElementById('result'), | |
| clone = oldnode.cloneNode(true); | |
| // work with the clone... | |
| // when you're done: | |
| oldnode.parentNode.replaceChild(clone, oldnode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment