jquery删除所有子元素并保留文本 [英] jquery remove all child elements and leave text

查看:86
本文介绍了jquery删除所有子元素并保留文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从div中删除所有子元素的最佳方法是什么,但是在jquery中保留直接在div中的任何文本。

Whats the best way to remove all child elements from a div but leave any text that is directly inside the div in jquery.

我试过.childre() .remove()但这是在firefox和opera中添加大量的空格。虽然工作正常,但是。

I have tried .childre().remove() but this is adding loads of whitespace in firefox and opera. Works fine in ie though.

推荐答案

尝试:

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

$。trim 应该删除来自文本的周围空白。

The $.trim is supposed to remove the surrounding whitespace from the text.

这篇关于jquery删除所有子元素并保留文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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