【问题标题】:Unbind <li> after click, then bind again once needed点击后解绑<li>,需要的时候再绑定
【发布时间】:2011-01-24 21:54:22
【问题描述】:

我创建了一个下拉菜单。当您单击“联系”时,会出现一个巨大的联系框。当我单击“关闭”时,它消失了。此功能有效。

我需要的是在表单打开后取消绑定列表项,但一旦关闭,再次启用它。

代码如下。

jQuery(document).ready(function() {

 jQuery("#header ul.menu li:last-child").addClass("open");

 jQuery("#header ul.menu li.open").click(function() {

  jQuery(this).unbind("click");

  jQuery("#contact").animate({marginTop:'+=426px'}, 2000);

  return false;

 });

 jQuery("#contact a#close").click(function() {

  jQuery("#contact").animate({marginTop:'-=426px'}, 2000);

  jQuery("#header ul.menu li:last-child").live("click", function() {

  });

  return false;

 });

});

【问题讨论】:

    标签: jquery list live bind unbind


    【解决方案1】:

    您应该指定对.unbind() / .bind() 的函数引用。

    function click_handler() { 
        // do something
    }
    
    jQuery("#header ul.menu li.open").click(function() {
       jQuery(this).unbind("click", click_handler);
    
       jQuery("#contact").animate({marginTop:'+=426px'}, 2000);
    
       return false;
    });
    
    // some code
    
    jQuery("#header ul.menu li.open").bind('click', click_handler);
    

    【讨论】:

      猜你喜欢
      • 2010-09-10
      • 2014-12-02
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多