How to get an element by its href in jquery?
I want to get an element by its href attribute in jquery o开发者_开发百科r javascript. Is that possible?
Yes, you can use jQuery's attribute selector for that.
var linksToGoogle = $('a[href="https://google.com"]');
Alternatively, if your interest is rather links starting with a certain URL, use the attribute-starts-with selector:
var allLinksToGoogle = $('a[href^="https://google.com"]');
If you want to get any element that has part of a URL in their href attribute you could use:
$( 'a[href*="google.com"]' );
This will select all elements with a href that contains google.com, for example:
- http://google.com
- http://www.google.com
- https://www.google.com/#q=How+to+get+element+by+href+in+jquery%3F
As stated by @BalusC in the comments below, it will also match elements that have google.com
at any position in the href, like blahgoogle.com
.
var myElement = $("a[href='http://www.stackoverflow.com']");
http://api.jquery.com/attribute-equals-selector/
Yes.
$('a[href=\'http://google.com\']')
http://api.jquery.com/attribute-equals-selector/
精彩评论