Best way to add DOM elements with jQuery

后端 未结 7 908
南笙
南笙 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:

     $("<div/>", {
    
      // 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: "<i>!!</i>",
    
      appendTo: "body"      // Finally, append to any selector
    
    }); 
    
    0 讨论(0)
提交回复
热议问题