jQuery —删除展开的文本但保留元素?
所以这是代码:
<parent><child>i want to keep the child</child>Some text I want to remove</parent>
我想将其更改为:
<parent><child>i want to keep the child</child></parent>
我看到很多人询问如何删除子元素并保留展开的文本,但不是相反。你如何瞄准那些没有命名的东西?
So here's the code:
<parent><child>i want to keep the child</child>Some text I want to remove</parent>
I'd like to change it to this:
<parent><child>i want to keep the child</child></parent>
I see lots of people asking how to remove the child element and preserve the unwrapped text, but not the other way around. How do you target something that isn't named?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
小提琴:http://jsfiddle.net/maniator/t2CDk/
如果你这样做丢失元素中的所有
事件侦听器
和数据。try this:
fiddle: http://jsfiddle.net/maniator/t2CDk/
if you do this you lose any
event listeners
and data from the elements.您可以使用老式的 javascript 方式来完成此操作,并测试每个节点的 nodeType 为 3,然后对其执行removeChild。这将非常干净和快速(最小的 jQuery),并且不会扰乱任何事件侦听器。
You can do it the ol' fashioned javascript way, and test each node for the nodeType of 3, then do a removeChild on it. This would be pretty clean and quick (minimal jQuery), and wouldn't mess up any event listeners.
我修改了尼尔斯的答案,使其适用于多个元素:
I modified Neals answer so it works for multiple elements:
非常简单,只需使用此代码:
这是完整的示例: https://blog.hfarazm .com/remove-unwrapped-text-jquery/
Very simple just use this code:
Here is full example: https://blog.hfarazm.com/remove-unwrapped-text-jquery/