我正在尝试过滤图像源中的特定单词(“无图像”),如果它返回 true,我想删除该特定图像但保留其余图像。
这是我的输出:
<div class="product">
<div class="image">
<img src="mytee-red.jpg">
<img src="mytee-blue.jpg">
<img src="mytee-black.jpg">
<img src="mytee-no-image.jpg">
</div>
</div>
这是我到目前为止所尝试过的,但似乎无法让它发挥作用:
var keyword = "no-image";
$(".product .image img").filter(function(index) {
if ($(this).attr("src") == keyword) {
$(this).remove();
}
});
任何帮助都会很棒!
最佳答案
您可以将其简化为单个命令 -
$(".product .image img[src*='no-image']").remove();
jQuery attribute contains selector将帮助您精确定位 src
属性中任何位置包含文本“no-image”的元素。
This is the most generous of the jQuery attribute selectors that match against a value. It will select an element if the selector's string appears anywhere within the element's attribute value.
关于jquery - 使用jquery过滤图像src中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12451787/