JQuery :包含以下单词之一

发布于 2024-12-29 13:20:45 字数 668 浏览 1 评论 0原文

在这一行中,我尝试选择包含这些单词之一但不包含图像的 ,但它不起作用,基本上当我在页面上使用它时,该页是空白的。

$('#SubscribersManageList tr:contains("CIUDAD EVITA"), tr:contains("MORENO"), tr:contains("CORRIENTES"), tr:contains("LA MATANZA"), tr:contains("QUILMES"), tr:contains("LOMAS DE ZAMORA"), tr:contains("LANUS"), tr:contains("AVELLANEDA"), tr:contains("CORDOBA"), tr:contains("CAPITAL FEDERAL"), tr:contains("RAMOS MEJIA"):not(:has(img[src*="images/plus.gif"]))').css("background-color", "red").insertAfter("tr.Heading3:last");

如果该行包含以下任一单词,该怎么做,也许以更简单的方式?

您可以在这里测试:http://jsfiddle.net/cwar3/1/

On this line I'm trying to select <tr>'s that contain either one of these words but doesn't contain an image but it's not working, basically when I use it on the page, the page is blank.

$('#SubscribersManageList tr:contains("CIUDAD EVITA"), tr:contains("MORENO"), tr:contains("CORRIENTES"), tr:contains("LA MATANZA"), tr:contains("QUILMES"), tr:contains("LOMAS DE ZAMORA"), tr:contains("LANUS"), tr:contains("AVELLANEDA"), tr:contains("CORDOBA"), tr:contains("CAPITAL FEDERAL"), tr:contains("RAMOS MEJIA"):not(:has(img[src*="images/plus.gif"]))').css("background-color", "red").insertAfter("tr.Heading3:last");

How to do it, perhaps in a simplier way, if the line contains either one of the following words?

You can test here: http://jsfiddle.net/cwar3/1/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2025-01-05 13:20:45

您要么必须将 #SubscribersManageList 与每个逗号分开,要么您可以像我下面所做的那样,并将其作为上下文传递一次。我可能会这样写(为了可读性而分成多行 - 你必须将它放回到一行才能使用它):

$('tr:contains("CIUDAD EVITA"), 
   tr:contains("MORENO"), 
   tr:contains("CORRIENTES"), 
   tr:contains("LA MATANZA"), 
   tr:contains("QUILMES"), 
   tr:contains("LOMAS DE ZAMORA"), 
   tr:contains("LANUS"), 
   tr:contains("AVELLANEDA"), 
   tr:contains("CORDOBA"), 
   tr:contains("CAPITAL FEDERAL"), 
   tr:contains("RAMOS MEJIA")', $("#SubscribersManageList"))
       .not('img[src*="images/plus.gif"]')
       .css("background-color", "red")
       .insertAfter("tr.Heading3:last");

根据你的评论,如果你想以特定的顺序将它们插入到 DOM 中,我建议这样做:

var containValues = [
   "CIUDAD EVITA",
   "MORENO", 
   "CORRIENTES", 
   "LA MATANZA", 
   "QUILMES", 
   "LOMAS DE ZAMORA", 
   "LANUS", 
   "AVELLANEDA", 
   "CORDOBA", 
   "CAPITAL FEDERAL", 
   "RAMOS MEJIA" 
];

var context = $("#SubscribersManageList");
var target = context.find("tr.Heading3:last");
for (var i = containValues.length - 1; i >= 0; i--) {
    context.find('tr:contains("' + containValues[i] + '")')
        .not('img[src*="images/plus.gif"]')
        .css("background-color", "red")
        .insertAfter(target);
}

您可以在这里看到它的工作原理: http://jsfiddle.net/jfriend00/MzgbV/

这将一次遍历一个数组,查找、设计样式,然后插入每个数组,并按数组顺序处理它们。它实际上向后遍历数组,因此最后插入的第一个结束。

You either have to have the #SubscribersManageList with every comma piece separately or you can do like I did below and pass it just once as the context. I would probably write it like this (broken up onto multiple lines only for readability here - you have to put it back on one line to use it):

$('tr:contains("CIUDAD EVITA"), 
   tr:contains("MORENO"), 
   tr:contains("CORRIENTES"), 
   tr:contains("LA MATANZA"), 
   tr:contains("QUILMES"), 
   tr:contains("LOMAS DE ZAMORA"), 
   tr:contains("LANUS"), 
   tr:contains("AVELLANEDA"), 
   tr:contains("CORDOBA"), 
   tr:contains("CAPITAL FEDERAL"), 
   tr:contains("RAMOS MEJIA")', $("#SubscribersManageList"))
       .not('img[src*="images/plus.gif"]')
       .css("background-color", "red")
       .insertAfter("tr.Heading3:last");

Per your comments, if you want to insert them back into the DOM in a specific order, I'd suggest this:

var containValues = [
   "CIUDAD EVITA",
   "MORENO", 
   "CORRIENTES", 
   "LA MATANZA", 
   "QUILMES", 
   "LOMAS DE ZAMORA", 
   "LANUS", 
   "AVELLANEDA", 
   "CORDOBA", 
   "CAPITAL FEDERAL", 
   "RAMOS MEJIA" 
];

var context = $("#SubscribersManageList");
var target = context.find("tr.Heading3:last");
for (var i = containValues.length - 1; i >= 0; i--) {
    context.find('tr:contains("' + containValues[i] + '")')
        .not('img[src*="images/plus.gif"]')
        .css("background-color", "red")
        .insertAfter(target);
}

You can see it work here: http://jsfiddle.net/jfriend00/MzgbV/.

This will go through the array one at a time, finding, styling and then inserting each one and will process them in the array order. It actually goes through the array backwards so the last one inserted ends up first.

策马西风 2025-01-05 13:20:45

此过滤器变得如此复杂,您可能需要考虑使用 jQuery.fn.filter 功能:

$('#SubscribersManageList tr').filter(function(){
  if($(this).has('img[src*="images/plus.gif"]')){
    return false;
  }
  return ($(this).text().search(/(CIUDAD EVITA|MORENO|CORRIENTES|etc)/) > -1);
})
.css("background-color", "red")
.insertAfter("tr.Heading3:last");

This filter is getting so complex that you might want to think about using the jQuery.fn.filter function:

$('#SubscribersManageList tr').filter(function(){
  if($(this).has('img[src*="images/plus.gif"]')){
    return false;
  }
  return ($(this).text().search(/(CIUDAD EVITA|MORENO|CORRIENTES|etc)/) > -1);
})
.css("background-color", "red")
.insertAfter("tr.Heading3:last");
沉鱼一梦 2025-01-05 13:20:45

您可以在 .filter() 调用中使用正则表达式,而不是使用 :contains

$(selector).filter(function() 
{
    return /some regex/.test( $(this).text() );
});

所以它看起来像:

$('#SubscribersManageList tr').filter(function() 
{
    return /CIUDAD EVITA|MORENO/.test( $(this).text() );
});

Instead of useing :contains, you can use a regular expression in a .filter() call:

$(selector).filter(function() 
{
    return /some regex/.test( $(this).text() );
});

So it would look like:

$('#SubscribersManageList tr').filter(function() 
{
    return /CIUDAD EVITA|MORENO/.test( $(this).text() );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文