完整HTML文档中的jQuery对象
是否可以将完整的HTML文档解析为完整的jQuery对象?当我尝试时,例如
Is it possible to parse a complete HTML document as a complete jQuery object? When I try, e.g.
var $tmp = $("<html><head><title>title</title></head><body><p id='test'>test</p></body></html>");
console.log($tmp);
我得到:
[+] [title, p#test] []
即一个将所有头部的孩子与身体的所有部分组合在一起的数组.是否无法保留包括html,head和body元素在内的结构?
i.e. an array combining all of the head's children with all of the body's. Is it not possible to retain the structure, including the html, head, and body elements?
jquery会删除html和body,因为在文档中只能有一个html和body. Brian的答案是您可以获得的最接近的答案,但只能在非IE浏览器上使用,因为IE不会解析非html标签.
jquery strips out html and body as there can only be one html and body in a document. Brian's answer is the closest you can get, but only on non IE browser as IE don't parse non html tag.
例如:
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?
How about replacing html and body with div class=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));