从节点中删除元素

发布于 2024-12-25 04:39:09 字数 440 浏览 0 评论 0原文

给定一个 Nokogiri::XML::Node 对象,如何删除它的一些 dom 节点?例如,假设我有一个 innerHtml 的对象:

hello world <b>this is in bold face</b> <div>this is inside a div</div> bye bye

如何删除

元素并获取 Nokogiri::XML::Node< /code> 对象,其 innerHtml 为:

hello world <b>this is in bold face</b>  bye bye

Given a Nokogiri::XML::Node object, how can I remove some of its dom nodes? For example, suppose I have an object whose innerHtml is:

hello world <b>this is in bold face</b> <div>this is inside a div</div> bye bye

How can I remove the <div> element and get a Nokogiri::XML::Node object whose innerHtml is:

hello world <b>this is in bold face</b>  bye bye

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2025-01-01 04:39:09

找到

并对其调用 remove 。例如:

>> node.to_html
=> "<div>hello world <b>this is in bold face</b> <div>this is inside a div</div> bye bye</div>"
>> node.at('div').remove; node.to_html
=> "<div>hello world <b>this is in bold face</b>  bye bye</div>"

Find the <div> and call remove on it. For example:

>> node.to_html
=> "<div>hello world <b>this is in bold face</b> <div>this is inside a div</div> bye bye</div>"
>> node.at('div').remove; node.to_html
=> "<div>hello world <b>this is in bold face</b>  bye bye</div>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文