我想通过 jquery 或 javascript 中的 href 属性获取元素。那可能吗?
问问题
162743 次
4 回答
212
是的,您可以为此使用 jQuery 的属性选择器。
var linksToGoogle = $('a[href="https://google.com"]');
或者,如果您的兴趣是链接以某个 URL开头,请使用attribute-starts-with选择器:
var allLinksToGoogle = $('a[href^="https://google.com"]');
于 2010-06-23T22:24:11.670 回答
19
如果您想获取在其 href 属性中包含部分 URL 的任何元素,您可以使用:
$( 'a[href*="google.com"]' );
这将选择包含 google.com 的 href 的所有元素,例如:
- 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
.
于 2012-06-29T16:50:35.127 回答
5
var myElement = $("a[href='http://www.stackoverflow.com']");
于 2010-06-23T22:23:52.743 回答
4
于 2010-06-23T22:23:33.650 回答