【发布时间】:2015-05-03 17:56:21
【问题描述】:
假设一个 jquery 对象可以从字符串初始化。处理 ajax 结果时经常会发生这种情况,即我正在尝试复制 http://api.jquery.com/jQuery.post/
但是,我看到了奇怪的行为:
function test() {
var content = $("<html><body><div>hello</div><div>world</div></body></html>");
alert("content.text() = " + content.text());
alert("content.html() = " + content.html());
}
第一个警报显示:content.text() = helloworld
第二个警报显示:content.html() = hello
这里发生了什么?
解决方案
感谢大家的解释。我最终添加了另一层<div> 以拥有<body> 的单个子级,如
<html>
<body>
<div> <=== added
<div>hello</div>
<div>world</div>
</div>
</body>
</html>
【问题讨论】:
标签: javascript jquery html dom