删除一个HTML标记,但是保留innerHtml
16lz
2021-01-22
I have some simple HTML which I need to strip simple formatting.
我有一些简单的HTML,我需要去除简单的格式。
A nice house was found in <b>Toronto</b>.
I need to remove the bold, but leave the sentence intact.
我需要去掉粗体,但要保持句子完整。
How is this possible in jQuery?
如何在jQuery中实现这一点?
5 个解决方案
#1
270
$('b').contents().unwrap();
This selects all <b>
elements, then uses .contents()
to target the text content of the <b>
, then .unwrap()
to remove its parent <b>
element.
它选择所有的元素,然后使用.contents()来针对的文本内容,然后使用.unwrap()删除其父元素。
For the greatest performance, always go native:
要想有最好的表演,一定要本土化:
var b = document.getElementsByTagName('b');
while(b.length) {
var parent = b[ 0 ].parentNode;
while( b[ 0 ].firstChild ) {
parent.insertBefore( b[ 0 ].firstChild, b[ 0 ] );
}
parent.removeChild( b[ 0 ] );
}
This will be much faster than any jQuery solution provided here.
这将比本文提供的任何jQuery解决方案都要快得多。
更多相关文章
- 如何选择两个h2之间的所有元素
- 千呼万唤 HTML 5 (2) - 区块元素
- 在中的元素的自定义子弹符号,这是一个普通字符,而不是一个图像
- JQuery选择器和操作得到的元素
- 是否有更快的方法来遍历HTMLDocument中的每一个元素呢?
- 在PHP中获取幕布元素ID的文本[重复]
- 使用jQuery随机化一系列div元素
- AngularJS使用双向数据绑定将img元素标签中的图像显示为源?
- 选择父母没有某个类的所有元素(使用'.not()')