简体   繁体   English

jQuery,在匹配的元素中搜索属性值的元素

[英]jQuery, search for element with value in attribute among the matched elements

I find a collection of a tags using jQuery like this: 我找到的集合a使用jQuery这样的标签:

itemsPresent = $('h3.zn-ItemTitle > a');

Later in my scripts I need to find the a within the matched elements with a specific url in its href attribute without making a new jQuery select. 稍后在我的脚本中,我需要在匹配的元素中找到a ,在其href属性中使用特定的url而不进行新的jQuery选择。

How can I do this? 我怎样才能做到这一点?

You can use the attribute equals selector: 您可以使用attribute equals selector:

itemsPresent = $('h3.zn-ItemTitle > a[href="http://your-url.com"]');

Alternatively, if you want to keep them as separate objects: 或者,如果要将它们保存为单独的对象:

var itemsPresent = $('h3.zn-ItemTitle > a'),
    filteredAnchors = itemsPresent.filter('[href="http://your-url.com"]');

Use .filter() to filter the Jquery object based on your desired conditions. 使用.filter()根据您所需的条件过滤Jquery对象。

Try, 尝试,

itemsPresent.filter(function(){   
  return $(this).attr('href') == 'something';
}) 

Or as BenM suggested you can use, 或者正如BenM建议您可以使用的那样,

itemsPresent.filter('[href="something"]');

You can filter the collection 您可以过滤集合

itemsPresent = $('h3.zn-ItemTitle > a');

var otherItems1 = itemsPresent.filter('[href="someUrl"]');

//or

var otherItems2 = itemsPresent.filter(function() {
    return this.href.indexOf('someURL') != -1;
});

You can use filter : 你可以使用filter

var $a = itemsPresent.filter(function() {
    return $(this).prop('href') == 'http://your-website.com';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM