是否可以将完整的 HTML 文档解析为完整的 jQuery 对象?当我尝试时,例如
var $tmp = $("<html><head><title>title</title></head><body><p id='test'>test</p></body></html>");
console.log($tmp);
I get:
[+] [title, p#test] []
即一个数组,将所有头部的子项与所有身体的子项组合起来。难道不能保留结构,包括 html、head 和 body 元素吗?
jquery 去掉了 html 和 body,因为文档中只能有一个 html 和 body。 Brian 的答案是您能得到的最接近的答案,但仅限于非 IE 浏览器,因为 IE 不解析非 html 标签。
例如:
var test = "<test>This is it</test>";
alert($(test).html()); // display This is it in non IE browser (working in 8-9?).
编辑:
用 div class=html/body 替换 html 和 body 怎么样?
var test = "<html><body><div>this is a test</div></body></html>";
test = test.replace(/(\/body|\/html)/i, "\/div")
.replace(/html/i, "div class='html'")
.replace(/body/i, "div class='body'");
console.log($(test));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)