unbind后如何重新绑定click事件? [英] how to rebind click event to anchor after unbind?

查看:365
本文介绍了unbind后如何重新绑定click事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在第一次点击后取消绑定锚点,但是当用户点击某个特定按钮时,我想将click事件重新绑定到此锚点

I want to unbind the anchor after the first click, but when user click a specific button, I want to rebind the click event to this 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函数运行良好,但绑定函数不起作用,为什么?以及如何使其工作?

the unbind function works well, but the bind function does not work, why? and how to make it work?

推荐答案

将您的点击功能存储在变量中,以便轻松重新分配。 。

Store your click function in a variable so that it can easily be re-assigned...

$(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
  });    
});

这篇关于unbind后如何重新绑定click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆