I am trying to remove the first wrapping tag of an image IF one exists
我试图删除图像的第一个包装标签如果存在
<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>
I've had a look at a number of options and I assume my approach is correct here:
我已经看了很多选项,我认为我的方法是正确的:
$(".feature img").closest('a').remove();
If I use the example above,it removes the image too which is not what I want of course.
如果我使用上面的例子,它也会删除图像,这当然不是我想要的。
3 个解决方案
#1
16
jQuery has a built-in function for it: unwrap
:
jQuery有一个内置函数:unwrap:
$(".feature a > img").unwrap();
unwrap
docs:
Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
从DOM中删除匹配元素集的父元素,将匹配的元素留在原位。
child(>)
selector docs:
子(>)选择器文档:
Description: Selects all direct child elements specified by "child" of elements specified by "parent".
描述:选择由“parent”指定的元素“child”指定的所有直接子元素。
Thanks @am not i am!
JSFiddle DEMO
谢谢@am不是我! JSFiddle DEMO
#3
0
You're correct, it will remove the element and any children elements. Try this though, save the image as a variable and replace the html of the div:
你是对的,它将删除元素和任何子元素。试试这个,将图像保存为变量并替换div的html:
var myImg = $('.feature img');
$('.feature').html(myImg);
#1
16
jQuery has a built-in function for it: unwrap
:
jQuery有一个内置函数:unwrap:
$(".feature a > img").unwrap();
unwrap
docs:
Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
从DOM中删除匹配元素集的父元素,将匹配的元素留在原位。
child(>)
selector docs:
子(>)选择器文档:
Description: Selects all direct child elements specified by "child" of elements specified by "parent".
描述:选择由“parent”指定的元素“child”指定的所有直接子元素。
Thanks @am not i am!
JSFiddle DEMO
谢谢@am不是我! JSFiddle DEMO
#2
#3
0
You're correct, it will remove the element and any children elements. Try this though, save the image as a variable and replace the html of the div:
你是对的,它将删除元素和任何子元素。试试这个,将图像保存为变量并替换div的html:
var myImg = $('.feature img');
$('.feature').html(myImg);