jQuery 如何选择具有特定 href 的所有锚点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4629863/
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 do I select all anchors with a specific href?
提问by WNRosenberg
I've got the following code to track pageviews for external links matching a particular URL.
我有以下代码来跟踪与特定 URL 匹配的外部链接的综合浏览量。
$("a").each(function(i){
if (
$(this).attr('href') == "http://example.com/external/link/" ||
$(this).attr('href') == "http://example.com/external/link"
) {
$(this).click(function(){
_gaq.push(['_trackPageview', '/external/pagename']);
});
}
});
This code works, but it's extremely inefficient for pages with a lot of links. Is there a way to use a selector to select all the anchors with matching hrefs instead of scanning through all the links on the page?
此代码有效,但对于具有大量链接的页面来说效率极低。有没有办法使用选择器来选择所有具有匹配 href 的锚点,而不是扫描页面上的所有链接?
回答by Wookai
You can use the Attribute Starts With Selector
您可以使用属性以选择器开头
$('a[href^="http://example.com/external/link"]').click(function() {
_gaq.push(['_trackPageview', '/external/pagename']);
});
回答by Marcus Whybrow
Yes
是的
$('a[href^="http://example.com/external/link"]').click(function() {});
Using the attribute selector you can look for a particular href
. Instead of the normal href=
you might expect, I have used href^=
which matches any element which starts withthe specified string.
使用属性选择器,您可以查找特定的href
. 与href=
您可能期望的正常情况不同,我使用了href^=
which 匹配以指定字符串开头的任何元素。
Also you do not need to use each
to bind to the click event of all the anchor tags you will select. click()
will automatically do this for you.
此外,您不需要使用each
绑定到您将选择的所有锚标记的点击事件。click()
将自动为您执行此操作。
$("a[href^="http://example.com/external/link"]").click(function(){
_gaq.push(['_trackPageview', '/external/pagename']);
});
回答by Kees C. Bakker
In jQuery getting all hrefs would be like:
在 jQuery 中,获取所有的 href 会像:
var href = 'http://www.google.com';
var elements = $('a[href=' + href + ']');
alert("Found: " + elements.length);
回答by Muhammad Tanweer
You can use attribute ends with selector as well if you need to get elements with some specific ending attribute. Something like this:
如果您需要获取具有某些特定结束属性的元素,您也可以使用带有选择器的属性结束。像这样的东西:
$('a[href$=your text]') == "your text"
Hope this helps someone.
希望这可以帮助某人。
回答by man.2067067
Another Simple way is:
另一种简单的方法是:
$('a[href="MY_ANCHOR_TEXT_HERE"]');
Gives the list of all the anchor tags in the current page. To get the anchor tags in a div ID you can use:
给出当前页面中所有锚标记的列表。要在 div ID 中获取锚标记,您可以使用:
$('#MY_DIV_ID a[href="MY_ANCHOR_TEXT_HERE"]')
To get the size use:
要获取大小,请使用:
$('#MY_DIV_ID a[href="MY_ANCHOR_TEXT_HERE"]').size()
回答by Ajay Malhotra
Here you can the code:
在这里你可以得到代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("a").each(function(){
if($(this).attr("href") == "http://example.com/external/link/")
{
$(this).hide();
}
});
});
</script>
<a href="http://example.com/external/link/">a website link</a>
回答by Ken Redler
$('a[href^="http://example.com/external/link"]').click( function(e){
// you do want to track which link was clicked, yes?
_gaq.push(['_trackPageview', $(this).attr('href') ]);
// suppress default click or you'll leave the page immediately:
e.preventDefault();
do_other_stuff_presumably_with_gaq();
});