How to effectively add content using jQuery

I am using jQuery to add a lot of text inside a tag. I find more text in the tag to add more slowly, and for large amounts of text it is too slow.

Is there a more efficient way to add text? For example, creating dummy child tags and setting their contents, rather than adding to the parent?

+3
source share
4 answers

Note this and this too: jQuery Anti-Patterns
And anyway:

  • do not execute .append () in a loop
  • do not join directly to the DOM
  • DocumentFragment
  • DOM
+6

DOM DocumentFragments, node, - "writing-efficient- Javascript"

+2

, , .innerHTML , jQuery append. , . bgcolor - , , , . html, eval'ing (jQuery, , , , innerHTML , ). Eval , , . , , , . , DOM (.. var this = document.getElementById("YourDomNode"), this , , document.getElementById). . , script, . , . JavaScript, html, :

/* Call this section after appending to the dom with innerHTML */
var f = function() {runScripts(document.getElementById('newDOM_Section'));};
setTimeout(f,1);

/* Stick the following in your main .js file */
var stringScript
var f
function runScripts(element) {
    var scripts = element.getElementsByTagName("script"); 
    var intScriptsCount = scripts.length
        for (var i = 0; i < intScriptsCount; i++) { 
        stringScript = scripts[i].text
            setTimeout(stringScript, 1); 
        }

}

DOM , . , . (onclick, onchange ..).

, dom, : none, , : inline (inline ) wahtever, .

- , , / .

+1

, , , .

0

Source: https://habr.com/ru/post/1729855/


All Articles