如何通过jquery中的href获取元素?

时间:2010-06-23 22:21:39

标签: javascript jquery html javascript-framework

我想通过jquery或javascript中的href属性获取一个元素。这可能吗?

4 个答案:

答案 0 :(得分:187)

是的,您可以使用jQuery的attribute selector

var linksToGoogle = $('a[href="http://google.com"]');

或者,如果您感兴趣的是使用某个网址开始,请使用attribute-starts-with选择器:

var allLinksToGoogle = $('a[href^="http://google.com"]');

答案 1 :(得分:16)

如果您想在其href属性中获取包含URL一部分的任何元素,您可以使用:

$( 'a[href*="google.com"]' );

这将选择包含google.com的href的所有元素,例如:

如@BalusC在下面的评论中所述,它也会匹配href中任何位置google.com的元素,例如blahgoogle.com

答案 2 :(得分:5)

var myElement = $("a[href='http://www.stackoverflow.com']");

http://api.jquery.com/attribute-equals-selector/

答案 3 :(得分:4)

$('a[href=\'http://google.com\']')

http://api.jquery.com/attribute-equals-selector/