我正在尝试删除图像的第一个包装标签如果存在
<div class="feature">
<a>
<img width="252" height="79" alt="" src="http://localhost:81/site/wp-
content/uploads/2011/12/home-highlights.jpg" title="home-highlights"
class="alignnone size-full wp-image-55">
</a>
</div>
我看过很多选项,我认为我的方法在这里是正确的:
$(".feature img").closest('a').remove();
如果我使用上面的示例,它也会删除图像,这当然不是我想要的。
最佳答案
jQuery 有一个内置函数:unwrap
:
$(".feature a > img").unwrap();
展开
docs :
Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
child(>)
选择器 docs :
Description: Selects all direct child elements specified by "child" of elements specified by "parent".
谢谢@am 不是我!
<强> JSFiddle DEMO
关于javascript - 使用 jQuery 从图像中删除包装 <a> anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9295292/