简体   繁体   中英

How to remove similar HTML tag close to parent using jquery?

Here is my HTML code:

<div class="img-area text-center frustrated">
<img class="delete" style="height: 12px; float:right; padding-right:5px; cursor: pointer;" src="/linkmo/images/closeicon.png" offset="2">
<a class="loc" href="#">
<img id="imagelocation2" class="imagelocation" style="height:110px; width:105px;" alt="" src="/linkmo/upload/224927120.jpg" offset="2">
</a>
</div>

When the user clicks on image having class delete then I change the src attribute in img tag with the ID imagelocation2 using ajax.

What I want is that when image src is changed the img tag having the class delete should be removed.

What I have tried in my ajax success function:

 $('#imagelocation' + id).attr('src', '/linkmo/images/placeholder.png');//Changes the src of img tag, works fine
 $('.delete img[offest='+id+'').remove();//Does not work
 $('#imagelocation' + id).parent().closest(".delete").remove();//Does not work

What I am doing wrong here?

USe like this find instead of closest

$('#imagelocation' + id).parent().find(".delete").remove();

Because closest search for the parent elements. While find search for child elements.

您可以使用.siblings()的目标与一流的图像.delete

$('#imagelocation' + id).siblings('.delete').remove();

You have a type in your javascript.

$('.delete img[offest='+id+'').remove();

should be

$('.delete img[offset='+id+']').remove();

offest -> offset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM