jQuery selector for links with # in href attribute

前端 未结 3 1670
耶瑟儿~
耶瑟儿~ 2020-12-29 03:44

I tried to use this jQuery selector:

$(\"a:has(href*=#)\").click(function() {
     alert(\'works\');
});  

but it doesn\'t seem to work. I

3条回答
  •  礼貌的吻别
    2020-12-29 04:06

    *= will filter attributes that contain the given string anywhere

    $("a[href*='#']").click(function() {
        alert('works');
    });
    

    Also note that

    $("a[href^='#']").click(function() {
        alert('works');
    });
    

    will select any anchor whose href starts with a #

提交回复
热议问题