[英]Remove wrapping <a> anchor tag from an image using jQuery
我试图删除图像IF的第一个包装标签
<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();
unwrap
文档 :
从DOM中删除匹配元素集的父元素,将匹配的元素保留在原位。
child(>)
选择器文档 :
描述:选择由“parent”指定的元素“child”指定的所有直接子元素。
谢谢@am不是我!
JSFiddle DEMO
解包方法是你想要的方法:
$(".feature a").children('img').unwrap();
你是对的,它将删除元素和任何子元素。 试试这个,将图像保存为变量并替换div的html:
var myImg = $('.feature img');
$('.feature').html(myImg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.