需要删除带有类的衣柜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")
});
最佳答案
使用.prev()
代替.closest()
$(this).prev('.img-preview').remove();
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.
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/