How to replace innerHTML of a div using jQuery?

前端 未结 13 1825
北恋
北恋 2020-11-22 11:57

How could I achieve the following:

document.all.regTitle.innerHTML = \'Hello World\';

Using jQuery where regTitle is my di

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 12:18

    Just to add some performance insights.

    A few years ago I had a project, where we had issues trying to set a large HTML / Text to various HTML elements.

    It appeared, that "recreating" the element and injecting it to the DOM was way faster than any of the suggested methods to update the DOM content.

    So something like:

    var text = "very big content";
    $("#regTitle").remove();
    $("
    " + text + "
    ").appendTo("body");

    Should get you a better performance. I haven't recently tried to measure that (browsers should be clever these days), but if you're looking for performance it may help.

    The downside is that you will have more work to keep the DOM and the references in your scripts pointing to the right object.

提交回复
热议问题