Best way to add DOM elements with jQuery

后端 未结 7 1061
南笙
南笙 2020-12-05 02:08

So I\'ve seen three ways to add html/DOM elements to a page. I\'m curious what the pros and cons are for each of them.

1 - Traditional JavaScript

7条回答
  •  悲&欢浪女
    2020-12-05 02:45

    You can try this:

     $("
    ", { // PROPERTIES HERE text: "Click me", id: "example", "class": "myDiv", // ('class' is still better in quotes) css: { color: "red", fontSize: "3em", cursor: "pointer" }, on: { mouseenter: function() { console.log("PLEASE... "+ $(this).text()); }, click: function() { console.log("Hy! My ID is: "+ this.id); } }, append: "!!", appendTo: "body" // Finally, append to any selector });

提交回复
热议问题