jquery - 使用jquery过滤图像src中的单词

标签 jquery regex string filter

我正在尝试过滤图像源中的特定单词(“无图像”),如果它返回 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/

相关文章:

regex - 使用正则表达式选择一组以空格分隔的单词

python - 有没有办法修剪/去除 pandas 数据帧的多列中的空格?

javascript - 点击链接时会发生什么

javascript - Jquery - 计算时间表小时数

javascript - string.split 与 RegExp\s

java find() 总是返回 true

javascript - JSON AJAX - 返回空对象

Jquery - 将点击事件处理程序添加到由自动完成生成的 <li>

java - 按特定子字符串拆分字符串

php - 性能方面的插值(直接插入字符串)VS拼接