Using a selector on href but ignoring GET variables
I want to be able to select all <a>
tags which exactly match a given URL. If my target URL is http://www.example.com/foo
, I want to match both:
<a href="开发者_如何转开发http://www.example.com/foo">Blah</a>
<a href="http://www.example.com/foo?a=b">Blah</a>
I can't just use $('a[href^="http://www.example.com/foo"]')
since this also matches http://www.example.com/foo/bar
.
Is this possible with selectors, or must I iterate over the links manually?
I believe this is what you are after...
$('a[href^="http://www.example.com/foo"]').filter('[href="http://www.example.com/foo"],[href^="http://www.example.com/foo?"]');
Basically, this should get all links that start with URL. Then filter then to be sure that only the ones exactly matching or exactly matching AND have GET parameters are returned.
Here's a jsfiddle proving it.
you could write your own selector. or, there's a plugin which allows you to use regexp: http://james.padolsey.com/javascript/regex-selector-for-jquery/
精彩评论