javascript - 使用 jquery 类删除衣柜图像

标签 javascript jquery

需要删除带有类的衣柜img的图像src并将其设置为隐藏。这是我到目前为止所尝试过的。

<img class="img-preview" src="http://www.abmuku.com/wp-content/uploads/2012/04/google-logo-small.jpg" />
<span> random stuff here </span>
<button class="delete"> Delete

</button>


$('.delete').on('click', function(){
    $(this).closest('.img-preview').remove();
    console.log("ha")
});

http://jsfiddle.net/tQ5vr/38/

最佳答案

使用.prev()代替.closest()

$(this).prev('.img-preview').remove();

.closest()

Description: For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

.prev()

Description: Get the immediately preceding sibling of each element in the set of matched elements. If a selector is provided, it retrieves the previous sibling only if it matches that selector.

要更改 src 并隐藏它,您可以使用

$(this).prev('.img-preview').attr('src','').hide();

关于javascript - 使用 jquery 类删除衣柜图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40098128/

相关文章:

javascript - 重写reactjs中的组件toString方法

javascript - 无法使用 jQuery 删除或添加类

javascript - 将 REST API 调用从 Angular 转换为 jQuery

javascript - Meteor 服务器在请求缓慢时自行重启

javascript - 在提交表单上显示模式,然后按提交表单

javascript - sap.tnt.sidenavigation 如何获取SAPUI5中导航列表项是隐藏还是显示?

jQuery removeClass 持续时间不起作用?

javascript - 带有英语和波斯语数字的波斯字符正则表达式

javascript - 很好地格式化网格

jquery - 如何在子节点使用JSTREE动态加载数据时显示加载图像?