[英]How to remove similar HTML tag close to parent using jquery?
Here is my HTML code: 这是我的HTML代码:
<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. 当用户单击具有类
delete
图像时,我将使用ajax更改ID为imagelocation2
img标签中的src属性。
What I want is that when image src is changed the img tag having the class delete
should be removed. 我想要的是,当更改图像src时,应
delete
具有delete
类的img标签。
What I have tried in my ajax success function: 我在ajax成功功能中尝试过的工作:
$('#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
喜欢这样
find
而不是closest
$('#imagelocation' + id).parent().find(".delete").remove();
Because closest
search for the parent elements. 因为
closest
搜索父元素。 While find
search for child elements. 在
find
搜索子元素。
您可以使用.siblings()
的目标与一流的图像.delete
:
$('#imagelocation' + id).siblings('.delete').remove();
You have a type in your javascript. 您的JavaScript类型。
$('.delete img[offest='+id+'').remove();
should be 应该
$('.delete img[offset='+id+']').remove();
offest -> offset 最差->偏移
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.