2014-03-04 140 views
2

我一直在努力,现在摸不着头脑了小时的奋力。使用jQuery条件选择

我想选择与属性开头的锚链接的“http:// localhost”的,并排除含有“可湿性粉剂管理员对它们的所有链接。

我可以选择开头的所有环节:与此的“http // localhost”的:

$internalLinks = $("a[href^='http://localhost']") 

这是我迄今为止排除了“可湿性粉剂管理员”链接,但它不” t工作:

$internalLinks = $("a:not[href*='wp-admin']a[href^='http://localhost']"), 

我错过了什么?

回答

4

你几乎得到了它。你要找的选择是这样的:

a[href^='http://localhost']:not([href*='wp-admin']) 

你造了两个错误是1)重复的标签名(a)和2)忘记了括号中:not()选择。

+0

啊!而已!我眼睛流血4小时。非常感谢! 我只是简单地遵循这里发布的内容:http://stackoverflow.com/questions/10687131/jquery-select-by-attribute-using-and-and-or-operator 我找不到任何文档如何正确命令它。是否有任何或是否只是你随着时间的推移而知道的事情? – Kenny

+0

@Kenny:订单通常无关紧要,但标签名称必须首先出现。 – BoltClock

3

我想选择与属性http:/localhost开始所有的锚链接和排除包含的所有链接“可湿性粉剂管理员' 在他们中。

鉴于您的情况相对复杂的我会考虑使用filter()代替:

$('a').filter(function() { 
    // starts with 'http://localhost' and does not contain 'wp-admin' 
    return this.href.indexOf('http://localhost') == 0 && 
     this.href.indexOf('wp-admin') == -1; 
}); 

在一个单一的表达所做的一切并不意味着你避免在页面上评估所有的锚,所以你可以根据以及让自己更容易。