从onClick事件捕获链接

时间:2015-06-25 14:23:20

标签: javascript jquery javascript-events

我们的“联系我们”页面位于支持目录下,因此我想测试联系我们是否在网址中,如果不做其他事情则做一件事。 “this”无效 - 因此如何获得正在执行的链接的完整价值,例如“www.example.com/support/cars.html”或“www.example.com/support/contact-us”。 html“

  jQuery('a[href*="www.example.com/support"]').click(function(event){
       event.preventDefault();
       if (this.indexOf("contact-us") > -1) {
           alert('contact us');
       } else {
           alert('support');
       }   });

由于

1 个答案:

答案 0 :(得分:1)

使用href attr。您可以使用attr()

获取该信息
jQuery('a[href*="www.example.com/support"]').click(function(event) {
    event.preventDefault();
    if (jQuery(this).attr('href').indexOf("contact-us") > -1) {
        alert('contact us');
    } else {
        alert('support');
    }
});