如何只使用jQuery删除标签? [英] How to remove only tag using jQuery?
问题描述
我想使用jQuery,
删除跨度我尝试了 .unwrap();
但它不工作。
< div>
< ul>
< li>< a href =#>< span> link< / span>< / a>< / li&
< li>< a href =#>< span> link< / span>< / a>< / li&
< / ul>
< / div>显然, unwrap
code>不工作,因为 span
只有文本节点在其中和jquery不处理文本节点太好...这是工作但你(你可以使用 jQuery.text
而不是 jQuery.html
,如果您确定 span
只包含文本):
$('li a span')replaceWith 'li a span')。html());
编辑:实际上,看起来 unwrap
如果您使用 jQuery.contents
来解决jquery无法直接选择文本节点:
$('li a span')。contents()。 unwrap();
I want to remove the span using jQuery,
I have tried the .unwrap();
but it's not working.
<div>
<ul>
<li><a href="#"><span>link</span></a></li>
<li><a href="#"><span>link</span></a></li>
</ul>
</div>
Obviously, unwrap
doesn't work as the span
s only have text nodes inside them and jquery doesn't handle text nodes too well... this works however (you could use also jQuery.text
instead of jQuery.html
if you're sure that the span
only contains text):
$('li a span').replaceWith($('li a span').html());
Edit: Actually, it seems that unwrap
works as well if you use jQuery.contents
to work around the jquery's inability to directly select text nodes:
$('li a span').contents().unwrap();
这篇关于如何只使用jQuery删除标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!