如何选择具有特定href的所有锚点? [英] How do I select all anchors with a specific href?
本文介绍了如何选择具有特定href的所有锚点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码来跟踪与特定网址匹配的外部链接的综合浏览量。
I've got the following code to track pageviews for external links matching a particular 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']);
});
}
});
这段代码可以工作,但对于链接很多的页面来说效率极低。有没有一种方法可以使用选择器来选择所有具有匹配hrefs的锚,而不是通过扫描页面上的所有链接?
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?
推荐答案
您可以使用属性以选择器启动
$('a[href^="http://example.com/external/link"]').click(function() {
_gaq.push(['_trackPageview', '/external/pagename']);
});
这篇关于如何选择具有特定href的所有锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文