jQuery document.createElement equivalent?

后端 未结 14 1856
暖寄归人
暖寄归人 2020-11-22 05:41

I\'m refactoring some old JavaScript code and there\'s a lot of DOM manipulation going on.

var d = document;
var odv = d.createElement(\"div\");
odv.style.di         


        
14条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 06:19

    Simply supplying the HTML of elements you want to add to a jQuery constructor $() will return a jQuery object from newly built HTML, suitable for being appended into the DOM using jQuery's append() method.

    For example:

    var t = $("
    "); $.append(t);

    You could then populate this table programmatically, if you wished.

    This gives you the ability to specify any arbitrary HTML you like, including class names or other attributes, which you might find more concise than using createElement and then setting attributes like cellSpacing and className via JS.

提交回复
热议问题