Javascript 当前网址&;链接匹配-使用div类检查
是否有一种方法可以测试当前URL位置是否与页面上的链接匹配,同时检查匹配的链接是否具有特定的div类 e、 g 当然可以:Javascript 当前网址&;链接匹配-使用div类检查,javascript,jquery,window.location,Javascript,Jquery,Window.location,是否有一种方法可以测试当前URL位置是否与页面上的链接匹配,同时检查匹配的链接是否具有特定的div类 e、 g 当然可以: 对代码的编辑:删除了不必要的jQuery(“*”)以及find,并删除了作为参数的$。(根据评论的建议)使用hasClass('aaa')。使用*不是很昂贵吗?您可以直接使用选择器a[href='“+window.location.href+'],而无需使用.find():)@Terry hahah是的,原始jquery非常残酷,我将编辑:pWhy您将$传递给函数?在这种情
对代码的编辑:删除了不必要的
jQuery(“*”
)以及find
,并删除了作为参数的$
。(根据评论的建议)使用hasClass('aaa')。使用*
不是很昂贵吗?您可以直接使用选择器a[href='“+window.location.href+']
,而无需使用.find()
:)@Terry hahah是的,原始jquery非常残酷,我将编辑:pWhy您将$
传递给函数?在这种情况下没有必要。您正在一个冗余的jquery函数中传递jquery。@GaryStorey没有捕捉到,已修复
jQuery(document).ready(function($){
// find a-href on page with matching current location URL
jQuery("*").find("a[href='"+window.location.href+"']").each(function(){
// if matching a-href contains class .aaa
// then BBB action
// else if matching a-href contains class .bbb
// then CCC action
})});
jQuery(document).ready(function(){
// find a-href on page with matching current location URL
jQuery("a[href='"+window.location.href+"']").each(function(){
if ($(this).hasClass('aaa')) {
// BBB
}
if ($(this).hasClass('bbb')) {
// CCC
}
});
});