jQuery 如何根据其 href 属性在页面中查找链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3093098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find a link in a page based on its href attribute?
提问by daviddarx
I am trying to find the first link of my page which url fit to "AAA"
(example).
我试图找到我的页面的第一个链接,它的 url 适合"AAA"
(示例)。
I first try to get the links with this href:
我首先尝试使用此 href 获取链接:
$('a[href$="AAA"]')
Then selec the first one:
然后选择第一个:
$('a[href$="AAA"]')[0]
Then target the title attribute of this link
然后针对这个链接的title属性
$('a[href$="AAA"]')[0].attr("title");
But all of this return me "undefined", at each step. How to do that?
但所有这些都让我在每一步都“未定义”。怎么做?
Example anchor:
示例锚:
<a href="contents/medias/images/news/news_test_big.jpg" title="Nouvelle réalisation en ligne 1 FR" target="_blank" class="imageLink">
回答by marcgg
How about:
怎么样:
$("a[href='AAA']").first().attr("title")
// or
$("a[href='AAA']:first").attr("title")
Then it depends if you want the href to be equal to 'AAA', contains it, starts/ends with it. In that case you wouldn't use href=
but href$=
for instance. Neil's answercontains the different types of tests you can use.
然后这取决于您是否希望 href 等于“AAA”、包含它、以它开始/结束。在那种情况下,您不会使用href=
但href$=
例如。Neil 的回答包含您可以使用的不同类型的测试。
If you could post your HTML code it would help as well.
如果您可以发布您的 HTML 代码,它也会有所帮助。
回答by Kobi
$('a[href$="AAA"]')
should work fine, but it searches for href
s that endwith AAA.
$('a[href$="AAA"]')
应该可以正常工作,但它会搜索以 AAA结尾的href
s 。
Try:
$('a[href="AAA"]')
- Equals AAA
Or:
$('a[href*="AAA"]')
- Contains AAA
尝试:
$('a[href="AAA"]')
- 等于 AAA
或:
$('a[href*="AAA"]')
- 包含 AAA
回答by tvanfosson
Assuming that you have a matching link (i.e., one that ends with AAA).
假设您有一个匹配的链接(即以 AAA 结尾的链接)。
$('a[href="AAA"]:first').attr('title');
should work.
应该管用。
The reason that you are getting undefined for attr
is that it isn't a jQuery object after you apply the indexer to the result of the query, it's the actual DOM element. To use attr
, it needs to still be a jQuery object. Using the :first
selector will reduce the results of the query to the first matching one. Since attr
will return the value of the first matching attribute, it's not strictly necessary, but I think it makes the intent clearer. If you go to set the attribute value, attr
will apply to all of the matching elements so, in that case, it's important -- thus I would use it in both places for clarity.
您获得 undefined 的原因attr
是在将索引器应用于查询结果后,它不是 jQuery 对象,而是实际的 DOM 元素。要使用attr
,它仍然需要是一个 jQuery 对象。使用:first
选择器会将查询结果减少到第一个匹配的结果。由于attr
将返回第一个匹配属性的值,因此不是绝对必要的,但我认为它使意图更清晰。如果你去设置属性值,attr
将应用于所有匹配的元素,所以在这种情况下,这很重要——因此为了清楚起见,我将在两个地方使用它。
回答by Ken Redler
The way you've written it, you're looking for an href that ends with"AAA". Is that your intent?
按照您的编写方式,您正在寻找以“AAA”结尾的 href 。这是你的意图吗?
$=
means "ends with"^=
means "starts with"~=
means "contains"
$=
意思是“以”结尾^=
意思是“开始于”~=
"包含"是什么意思
References: jQuery Selectors
参考资料:jQuery 选择器