简体   繁体   English

Jquery href click - 如何启动事件?

[英]Jquery href click - how can I fire up an event?

I have this anchor and on click I would like to popup something. 我有这个锚点击并点击我想弹出一些东西。 This href is within a page that has other hrefs. 此href位于具有其他href的页面内。

<a class="sign_new" href="#sign_up">Sign up</a>

jQuery: jQuery的:

$(document).ready(function(){
   $('a[href = "sign_up"]').click(function(){
      alert('Sign new href executed.'); 
   }); 
});

The above code does not fire up. 上面的代码没有启动。

It doesn't because the href value is not sign_up .It is #sign_up . 它不是因为href值不是sign_up它是#sign_up Try like below, You need to add "#" to indicate the id of the href value. 尝试如下,你需要添加“#”来表示href值的id。

$('a[href="#sign_up"]').click(function(){
  alert('Sign new href executed.'); 
}); 

DEMO: http://jsfiddle.net/pnGbP/ 演示: http //jsfiddle.net/pnGbP/

If you own the HTML code then it might be wise to assign an id to this href. 如果您拥有HTML代码,那么为此href分配id可能是明智之举。 Then your code would look like this: 然后你的代码看起来像这样:

<a id="sign_up" class="sign_new">Sign up</a>

And jQuery: 和jQuery:

$(document).ready(function(){
    $('#sign_up').click(function(){
        alert('Sign new href executed.');
    });
});

If you do not own the HTML then you'd need to change $('#sign_up') to $('a.sign_new'). 如果您不拥有HTML,那么您需要将$('#sign_up')更改为$('a.sign_new')。 You might also fire event.stopPropagation() if you have a href in anchor and do not want it handled (AFAIR return false might work as well). 如果你在锚中有一个href并且不想处理它,你也可能触发event.stopPropagation()(AFAIR返回false也可以工作)。

$(document).ready(function(){
    $('#sign_up').click(function(event){
        alert('Sign new href executed.');
        event.stopPropagation();
    });
});

Try: 尝试:

$(document).ready(function(){
   $('a .sign_new').click(function(){
      alert('Sign new href executed.'); 
   }); 
});

You've mixed up the class and href names / selector type. 你已经混淆了classhref名称/选择器类型。

You are binding the click event to anchors with an href attribute with value sign_new . 您将click事件绑定到具有值sign_newhref属性的sign_new

Either bind anchors with class sign_new or bind anchors with href value #sign_up . 使用类sign_new绑定锚点或使用href值绑定锚点#sign_up I would prefer the former. 我更喜欢前者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM