`
kabike
  • 浏览: 598096 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

jquery创建元素的方式

    博客分类:
  • html
阅读更多
原来一直以为jquery的$()里只能放选择器,原来也可以放html字符串来创建新的元素.不过创建元素的时候用的是innerHTML的方式呢,还是createElement呢?
文档上说
引用

If the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's innerHTML mechanism. In most cases, jQuery creates a new <div> element and sets the innerHTML property of the element to the HTML snippet that was passed in. When the parameter has a single tag (with optional closing tag or quick-closing) — $( "<img />" ) or $( "<img>" ), $( "<a></a>" ) or $( "<a>" ) — jQuery creates the element using the native JavaScript createElement() function

也就是说,包含了属性的用的是innerHTML,比如
$("<p id='test'>My <em>new</em> text</p>").appendTo( "body" );


不包含属性的,是createElement,比如
$("<img>")
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics