开发者

jquery how to remove the wrapping div?

开发者 https://www.devze.com 2023-02-27 10:37 出处:网络
i have this: <div class=\"test\"><div id=\"123\"><img class=\"image\" src=\"1.jpg\"></div></div>开发者_StackOverflow社区

i have this:

<div class="test"><div id="123"><img class="image" src="1.jpg"></div></div>开发者_StackOverflow社区
<div class="test"><div id="123"><img class="image" src="2.jpg"></div></div>

what i want to do is if(img.attr("src") == ("1.jpg"){ remove the entire div that hosts that image} to become like this:

div class="test"><div id="123"><img class="image" src="2.jpg"></div></div>

the divs have the same class. thanks


EDIT Updated answer to reflect changes in question

One line is all it takes :)

$('img[src="1.jpg"]').parents('div.test').remove();

http://api.jquery.com/parents/

Or

$('img[src="1.jpg"]').closest('div.test').remove();

http://api.jquery.com/closest/


Using .closest() with the div selector would work for you.


$("img[src='1.jpg']").parent().remove();


$("img[src='1.jpg']").parent().remove();

<div class="test"><div id="123"><img class="image" src="1.jpg"></div></div>

$("img[src='1.jpg']").parent("div").parent("div.test").remove();


Removes the parent div for the image where it's src = '1.jpg'

$('img[src="1.jpg"]').closest('div.test').remove();
0

精彩评论

暂无评论...
验证码 换一张
取 消