我可以使用JQuery操纵外部HTML文档吗? [英] Can i manipulate an external HTML document with JQuery?

查看:117
本文介绍了我可以使用JQuery操纵外部HTML文档吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清理一个HTML文档(在Google文档中创建),以便我可以在我的CMS上发布。

I would like to sanitize a HTML document (created in google docs) so I can publish it on my CMS.

我的源文档是一个字符串,从头到尾,头部,风格,身体等。我想提取身体内容,替换/消除几个标签。如果我可以使用jQuery来做到这一点,我认为这将比使用更复杂的html解析器更容易。

I have the source document in a string, from to , with header, style, body etc. I would like to extract the body content and replace/eliminate a few tags. If I could do this using jQuery I think it would be easier than with more sophisticated html parsers.

但是当我尝试获取文档的正文时,得到可用的结果。我试过:

But when I try to get the body of the document, I don't get usable results. I tried:

var gdoc = "<html>...google document...</html>"
$(gdoc) //list of text nodes, can not rebuild to document or find body
$("body",gdoc) //empty list

这是可行还是我完全错了?
您可以分享的任何提示/引用?

Is this doable or am i going completely wrong about this? Any tips / references you could share?

推荐答案

尝试这样:

var gdoc = '<html><body><div id="foo">Bar</div></body></html>';
var data = $('<div/>').html(gdoc).find('#foo').html();
alert(data);

演示

这篇关于我可以使用JQuery操纵外部HTML文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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