Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 管理jquery单击事件_Javascript_Jquery_Html - Fatal编程技术网

Javascript 管理jquery单击事件

Javascript 管理jquery单击事件,javascript,jquery,html,Javascript,Jquery,Html,我用以下方法捕获“a”元素中的所有点击: $("a").click(function(e){....}); 我想知道是否可以根据某个变量放弃某些事件 例如 If href="http://www.google.es" then alert("google.es") else if href="http://www.google.com" then do not handle the event and let the browser do whatever it has to do. 我不

我用以下方法捕获“a”元素中的所有点击:

$("a").click(function(e){....});
我想知道是否可以根据某个变量放弃某些事件

例如

If href="http://www.google.es" then alert("google.es")

else if href="http://www.google.com" then do not handle the event and let the browser do whatever it has to do.
我不知道我是否解释得很好

$("a").click(function(e){
    e.preventDefault();
    if (this.href == "http://www.google.es/") alert("google.es");
    else if (this.href == "http://www.google.com/") window.location = this.href;
});

Fiddle Demo:

在函数$(this).attr('href')中应该提供锚定标记的href,然后您可以在上述示例的其余逻辑中使用它

$('a[href*=.es]').click(function(e){
e.preventDefault();
});
将使href属性中包含.es的所有链接都不被遵循,而保留其他链接。

或者,您可以执行以下操作:

$("a").click(function(e) {
   if ($(this).attr('href') == 'http://www.google.es') {
      alert('google.es');
      e.preventDefault();
   }
   // if code gets here, it's ok for the browser to handle normally.
}
$("a[href!='http://www.google.com']").click(function(e) {
    e.preventDefault();
    if(this.getAttribute('href') === 'http://www.google.es') alert("google.es");
});

首先不要为
google.com
生成点击事件。

到目前为止发布的所有建议都会起作用,但它们不是最灵活的。如果您想匹配任何以
google.es
作为主机名的链接,我会这样做:

$(document.body).delegate('a', 'click', function(e) {
    var hostname = this.hostname || $(this).prop('href').split('/')[1];

    if (hostname == 'www.google.es') {
        alert('google.es');
        e.preventDefault();
    }
});

谢谢,事实上,我认为它的工作原理就像它总是使用e.prevenFeault()行为一样。@lonesomeday:这取决于
href
属性中的确切内容。我只是按照@sergi所说的去做:
If href=”http://www.google.es“那么提醒一下(“google.es”)
。哦,我同意。然而,提问者所说的和他们的意思经常不同!