jQuery .append()函数 [英] jQuery .append() function
本文介绍了jQuery .append()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么此
$("#mydiv").append("<ul>");
$("#mydiv").append("<li>Hello</li>");
$("#mydiv").append("</ul>");
alert($("#mydiv").html());
产生
<ul></ul><li>Hello</li>
不是
<ul><li>Hello</li></ul>
?
谢谢!
推荐答案
因为浏览器需要在每次追加之后重新构建其DOM.它不知道结束标记会在以后出现,而开始标记本身就是无效的,因此开始进行纠错,在这种情况下,错误标记会关闭未结束的元素.
Because the browser needs to (re)build its DOM after each append. It can't know that a closing tag will come later, and an opening tag by itself is invalid, so error correction kicks in which in this case closes the unclosed element.
这是innerHtml
及其依赖的内容(例如jQuery的append方法)不可靠的原因之一,应尽可能避免.
This is one of the reasons why innerHtml
and things that rely on it (such as jQuery's append method) are not reliable and should be avoided when possible.
这篇关于jQuery .append()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文