简体   繁体   English

jquery删除所有子元素并保留文本

[英]jquery remove all child elements and leave text

Whats the best way to remove all child elements from a div but leave any text that is directly inside the div in jquery. 什么是从div中删除所有子元素的最佳方法,但是在jquery中保留直接位于div内的任何文本。

I have tried .childre().remove() but this is adding loads of whitespace in firefox and opera. 我试过.childre()。remove()但这是在firefox和opera中添加大量的空格。 Works fine in ie though. 尽管如此,工作正常。

Try: 尝试:

$('#element_id').children().remove().end().text($.trim($('#element_id').text()));

The $.trim is supposed to remove the surrounding whitespace from the text. $.trim应该从文本中删除周围的空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM