使用jQuery从图像中删除包装<a>锚标记

Six*_*mes 9 html javascript css anchor 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>
Run Code Online (Sandbox Code Playgroud)

我已经看了很多选项,我认为我的方法是正确的:

$(".feature img").closest('a').remove();
Run Code Online (Sandbox Code Playgroud)

如果我使用上面的例子,它也会删除图像,这当然不是我想要的.

gdo*_*ica 16

jQuery有一个内置函数unwrap:

$(".feature a > img").unwrap();
Run Code Online (Sandbox Code Playgroud)

unwrap docs:

从DOM中删除匹配元素集的父元素,将匹配的元素保留在原位.

child(>)选择器文档:

描述:选择由"parent"指定的元素"child"指定的所有直接子元素.

谢谢@am不是我!
JSFiddle DEMO


Ric*_*lly 6

解包的方法是你想要的:

 $(".feature a").children('img').unwrap();
Run Code Online (Sandbox Code Playgroud)