JQuery: Build HTML in 'memory' rather than DOM

前端 未结 7 969
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 21:43

Is there a way to \'pre-build\' a snippet of HTML before adding it to the DOM?

For example:

$mysnippet.append(\"

hello

\"); $mysni
相关标签:
7条回答
  • 2020-11-29 22:28

    You can prebuild it and also attach events, as well as data attributes, inner html, etc, etc.

    var eltProps = {
        css: {
            border: 1,
            "background-color": "red",
            padding: "5px"
        },
        class: "bblock",
        id: "bb_1",
        html: "<span>jQuery</span>",
        data: {
            name: "normal-div",
            role: "building-block"
        },
        click: function() {
             alert("I was clicked. My id is" + $(this).attr("id"));
        }
    };
    
    var elt = $("<div/>", eltProps);
    
    $("body").append(elt);
    
    0 讨论(0)
提交回复
热议问题