原来一直以为jquery的$()里只能放选择器,原来也可以放html字符串来创建新的元素.不过创建元素的时候用的是innerHTML的方式呢,还是createElement呢?
文档上说
[quote]
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
[/quote]
也就是说,包含了属性的用的是innerHTML,比如
不包含属性的,是createElement,比如
文档上说
[quote]
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
[/quote]
也就是说,包含了属性的用的是innerHTML,比如
$("<p id='test'>My <em>new</em> text</p>").appendTo( "body" );不包含属性的,是createElement,比如
$("<img>")
本文探讨了jQuery中使用$()函数创建HTML元素的方法。当创建简单的单标签元素时,jQuery利用JavaScript的createElement()方法;而对于带有属性的复杂HTML片段,则通过innerHTML机制由浏览器处理。
875

被折叠的 条评论
为什么被折叠?



