完整HTML文档中的jQuery对象 [英] jQuery object from complete HTML document

查看:70
本文介绍了完整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));

这篇关于完整HTML文档中的jQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆