我可以使用 JQuery 操作外部 HTML 文档吗?
我想清理 HTML 文档(在 google 文档中创建),以便我可以将其发布到我的 CMS 上。
我有一个字符串形式的源文档,从 到 ,带有标题、样式、正文等。我想提取正文内容并替换/删除一些标签。如果我可以使用 jQuery 来做到这一点,我认为它会比使用更复杂的 html 解析器更容易。
但是当我尝试获取文档正文时,我没有得到可用的结果。我尝试过:
var gdoc = "<html>...google document...</html>"
$(gdoc) //list of text nodes, can not rebuild to document or find body
$("body",gdoc) //empty list
这是可行的还是我对此完全错误? 您可以分享任何提示/参考吗?
I would like to sanitize a HTML document (created in google docs) so I can publish it on my CMS.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试像这样:
演示。
Try like this:
Demo.
我相信你可以做你想做的事,但你的措辞不正确。您可以从另一个文档中获取 HTML 并对其进行操作,但无法操作外部文档本身。你可以使用它来抓取它
I believe you can do what you're trying to do, but you're wording it improperly. You can grab the HTML from another document and manipulate it, but you can't manipulate the external document persay. You can grab it using