Appending large block of html with append()

后端 未结 11 952
-上瘾入骨i
-上瘾入骨i 2021-01-31 10:09

Im trying to append a large block of text using jquery\'s append().

$(\'#add_contact_btn\').click(function(event) {
    event.preventDefault();

    var large =         


        
11条回答
  •  花落未央
    2021-01-31 10:28

    You should create a template in HTML that is hidden, then append its content HTML. For example:

    Some HTML

    jQuery:

    $("#container").append($("#template").html());
    

    Putting HTML in a JavaScript string is harder to read and search for, is error prone and your IDE will struggle to format it properly.


    Update 2019

    Check out the template tag, which was created for this purpose: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template

    The template tag is even allowed to contain what would be invalid HTML elsewhere, e.g. a td tag outside a table.

提交回复
热议问题