Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
Jquery 停止做e.preventDefault();第二次点击?_Jquery - Fatal编程技术网

Jquery 停止做e.preventDefault();第二次点击?

Jquery 停止做e.preventDefault();第二次点击?,jquery,Jquery,就我个人而言,我似乎无法破解这个。我对jQuery相当熟悉,但我只想: 我正在处理的链接指向/mypagename 第一次单击a.nav_hidepanel时,在#面板上启动动画,但不允许链接正常工作。但是,请添加一个替换类.nav_returnlink,以便我们下次可以将其作为目标 现在,第二次单击此链接时,它有一个不同的类,因此我们希望允许它像往常一样工作,并将用户发送到/mypagename 禁用链接没有问题,但返回true似乎就是不起作用!我错过了什么明显的东西吗 jQuery(docu

就我个人而言,我似乎无法破解这个。我对jQuery相当熟悉,但我只想:

我正在处理的链接指向/mypagename

  • 第一次单击a.nav_hidepanel时,在
    #面板
    上启动动画,但不允许链接正常工作。但是,请添加一个替换类
    .nav_returnlink
    ,以便我们下次可以将其作为目标
  • 现在,第二次单击此链接时,它有一个不同的类,因此我们希望允许它像往常一样工作,并将用户发送到/mypagename
  • 禁用链接没有问题,但
    返回true似乎就是不起作用!我错过了什么明显的东西吗

    jQuery(document).ready(function(){
        $("a.nav_hidepanel").click(function(e){
            e.preventDefault();
            $("#panel").animate({marginLeft:"-547px"}, 500 );
            $(this).removeClass('nav_hidepanel');
            $(this).addClass('nav_returnlink');
        });
    
        $("a.nav_returnlink").click(function(){
            return true;
        });
    });
    

    为什么不改用计数器?

    var clickCount = 0;
    $("a.nav_hidepanel").click(function(e){
        if(clickCount == 0) {
            e.preventDefault();
            $("#panel").animate({marginLeft:"-547px"}, 500 );
        }
        clickCount++;
    });
    

    尝试使用jquery在live Function中添加单击事件

    $("a.nav_returnlink").live("click",function(){
       return true;
    });
    

    由于元素在第一次单击后将具有
    nav_returnlink
    类,只需检查它是否存在:

    jQuery(document).ready(function(){
        $("a.nav_hidepanel").click(function(e){
            //This will return true after the first click 
            //and preventDefault won't be called.
            if(!$(this).hasClass('nav_returnlink'))
                e.preventDefault();
    
            $("#panel").animate({marginLeft:"-547px"}, 500 );
            $(this).removeClass('nav_hidepanel');
            $(this).addClass('nav_returnlink');
        });
    });
    

    您始终可以解除事件处理程序的绑定

    var clickCancel = function(event) {
        event.preventDefault();
        $(this).unbind("click",clickCancel);
        $("#panel").animate({marginLeft:"-547px"}, 500 );// you add your animation in this function, it will only be called on  the first click..
    
    };
    $("a.nav_hidepanel").bind("click", clickCancel);
    

    以下是代码不起作用的原因的简短解释:

    • 将.nav_hidepanel上的单击事件绑定到preventDefault这是可以的
    • 将单击事件绑定到当时不存在的元素a.nav_returnlink这是一个问题
    • 在第一次单击回调时,您调用e.preventDefault(),因为此单击事件从未解除绑定,所以此链接将永远不会执行其默认操作这又是一个问题
    这里有一个可能的解决方案

    jQuery(document).ready(function(){
      $("a.nav_hidepanel").click(function(e){
        e.preventDefault();
        $("#panel").animate({marginLeft:"-547px"}, 500 );
        $(this).removeClass('nav_hidepanel');
        $(this).addClass('nav_returnlink');
        $(this).unbind('click'); //unbind the click event so this is not called again
      });
      $("a.nav_returnlink").live(function(){ //use a live event to bind to this element, that is available in the future
          return true;
      });
    });
    
    另一种解决方案是在第一次回调中绑定新的click事件:

    jQuery(document).ready(function(){
      $("a.nav_hidepanel").click(function(e){
        e.preventDefault();
        $("#panel").animate({marginLeft:"-547px"}, 500 );
        $(this).removeClass('nav_hidepanel');
        $(this).addClass('nav_returnlink');
        $(this).unbind('click');
    
        $(this).click(function(){
            //do whatever you like to do
        });
      });
    });
    

    你好,希望这篇文章能帮助你


    这也可以解决问题。。。不过,我仍然认为正确的方法是在使用完事件处理程序后删除它。我认为使用计数器有点笨拙?@user9980是的,我想你可以使用布尔值,但似乎你已经找到了解决方案。肯定比我插入的代码更精简:)我理解你为什么这么做关于何时阻止默认操作,我们也做了更具体的说明。谢谢!