如何只使用jQuery删除标签? [英] How to remove only tag using jQuery?

查看:193
本文介绍了如何只使用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 spans 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());

Working example

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屋!

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