当前位置: 代码迷 >> Web前端 >> jquery创造元素的方式
  详细解决方案

jquery创造元素的方式

热度:353   发布时间:2013-02-24 17:58:57.0
jquery创建元素的方式
原来一直以为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>")
  相关解决方案