jquery - 解除绑定(bind)后如何重新绑定(bind)点击事件到 anchor ?

标签 jquery bind anchor unbind

我想在第一次点击后解​​除 anchor 的绑定(bind),但是当用户点击特定按钮时,我想将点击事件重新绑定(bind)到该 anchor

这段代码是我写的

 $(document).ready(function(){
    $("a.package").click(function(){
        //alert('click');            
        $(this).unbind('click'); 
        // the rest of the code
    });

    $('#activate').click(function(){
        $('a.package').bind('click');
        // the rest of the code
    });
});

unbind 功能可以正常使用,但是bind 功能却不能使用,为什么?以及如何让它发挥作用?

最佳答案

将您的点击函数存储在变量中,以便可以轻松地重新分配...

$(document).ready(function() {
  var onclick = function(e) {
    //alert('click');            
    $(this).unbind('click'); 
    // the rest of the code
  }

  $("a.package").click(onclick);

  $('#activate').click(function() {
    $('a.package').click(onclick);
    // the rest of the code
  });    
});

关于jquery - 解除绑定(bind)后如何重新绑定(bind)点击事件到 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6616214/

相关文章:

jquery - 如何将动态生成的 anchor 标记的 URL 传递给 jQuery hide() 函数?

jquery - 使用 jQuery 打开新选项卡中的所有链接

javascript - 事件处理程序未拾取 Keydown 事件

jquery - 自定义 HTML5 范围输入和句柄偏移

java - 当 TableView 为空时禁用按钮

jquery - 为什么.load是绑定(bind)事件?

javascript - 如何在 React.js 中的按钮上选中/取消选中带有 onClick 事件的复选框?

javascript - 为什么我不能在亚马逊(控制台)中使用 jQuery?

java - 使用 JSch 连接到 SFTP 时如何选择网络接口(interface)

internet-explorer - 当 span 是 anchor 的子项时,不显示标题工具提示