我想在jquery或javascript中通过href属性获取元素,这可行吗?
我想在jquery或javascript中通过href属性获取元素,这可行吗?
是的,您可以使用jQuery的属性选择器来实现这一点。
var linksToGoogle = $('a[href="https://google.com"]');
或者,如果您感兴趣的是以某个URL开头的链接,可以使用 attribute-starts-with 选择器:
var allLinksToGoogle = $('a[href^="https://google.com"]');
如果你想获取任何href属性中包含部分URL的元素,你可以使用:
$( 'a[href*="google.com"]' );
这将选择所有带有包含google.com的href属性的元素,例如:
blahgoogle.com
。var myElement = $("a[href='http://www.stackoverflow.com']");
blahgoogle.com
和some.com?blah=google.com
。这是一个糟糕的建议。 - BalusC