简体   繁体   English

jQuery单击触发两次

[英]jQuery click fires twice

I have this jsfiddle: http://jsfiddle.net/us28bg4u/1/ 我有这个jsfiddle: http : //jsfiddle.net/us28bg4u/1/

How come, that, when I press "First" -> "Left" the action is only fired once. 怎么了,当我按"First" -> "Left" ,动作只触发一次。 But when I do it again, the action is fired twice, and third time I press the same, it fires three times and so on. 但是,当我再次执行此操作时,该动作将触发两次,第三次按相同的动作时,它将触发三次,依此类推。

I cant figure out why it is stacking up. 我不知道为什么它堆积。 Can someone enlighten me? 有人可以启发我吗? :) :)

I have tried with: 我尝试过:
e.stopPropagation();
e.preventDefault();
- but nothing seems to prevent the clicks for stacking up. -但似乎没有什么可以阻止点击次数的累积。

my js looks like this: 我的js看起来像这样:

    var side = '';
    var action = '';

    $(document).ready(function () {
        $(".first").click(function (e) {
            logit("First pressed");
            preStart('first');
        });

        $(".second").click(function (e) {
            logit('Second pressed');
            preStart('second');
        });

        function preStart(action) {
            $("#overlay").fadeIn(200);

            $(".leftside").click(function (e) {
                side = "left";
                $("#overlay").fadeOut(200);

                logit('Starting ' + action + ' (' + side + ')');
             });

            $(".rightside").click(function (e) {
                side = "right";
                $("#overlay").fadeOut(200);

                logit('Starting ' + action + ' (' + side + ')');
            });
        }

        function logit(logtxt){
            $("#log").append("<li>"+logtxt+"</li>");

        }
    });

Has it something to do with the click() functions being in another function? 与click()函数在另一个函数中有关吗?

You are binding handlers to the events inside the click handler that's why it's happening, 您正在将处理程序绑定到click处理程序内部的事件,这就是发生这种情况的原因,

Do it like bellow 像波纹管一样做

    var side = '';
    var action = '';

    $(document).ready(function () {
        $(".first").click(function (e) {
            logit("First pressed");
            preStart('first');
        });

        $(".second").click(function (e) {
            logit('Second pressed');
            preStart('second');
        });

        $(".leftside").click(function (e) {
                side = "left";
                $("#overlay").fadeOut(200);

                logit('Starting ' + action + ' (' + side + ')');
             });

            $(".rightside").click(function (e) {
                side = "right";
                $("#overlay").fadeOut(200);

                logit('Starting ' + action + ' (' + side + ')');
            });

        function preStart(action) {
            $("#overlay").fadeIn(200);
        }

        function logit(logtxt){
            $("#log").append("<li>"+logtxt+"</li>");

        }
    });

FIXED DEMO 固定演示

Event bindings can stack. 事件绑定可以堆叠。 Inside of preStart clear the previous binding by adding .unbind() into the method chain before the event is bound again like so: preStart内部,通过在事件再次绑定之前将.unbind()添加到方法链中preStart清除先前的绑定,如下所示:

    function preStart(action) {
        $("#overlay").fadeIn(200);

        $(".leftside").unbind("click").click(function (e) {
            side = "left";
            $("#overlay").fadeOut(200);

            logit('Starting ' + action + ' (' + side + ')');
         });

        $(".rightside").unbind("click").click(function (e) {
            side = "right";
            $("#overlay").fadeOut(200);

            logit('Starting ' + action + ' (' + side + ')');
        });
    }

Handle the click event with OWN parameter Like this. 像这样用OWN参数处理click事件。 Try this one, 试试这个

 $(".leftside").click(function (e) {
     if(!e.handled){
         e.handled = true;
         side = "left";
         $("#overlay").fadeOut(200);

         logit('Starting ' + action + ' (' + side + ')');
     }

});

$(".rightside").click(function (e) {
      if(!e.handled){
            e.handled = true;
             side = "right";
             $("#overlay").fadeOut(200);

            logit('Starting ' + action + ' (' + side + ')');
       }   

  });

Update Fiddle 更新小提琴

It depends how many times you invoke preStart, when you click first you bind $(".leftside").click() $(".rightside").click() once, as you click through first or second one more time you created another binding on $(".leftside") and $(".rightside"), so on so forth. 这取决于您调用preStart的次数,第一次单击时,将$(“。leftside”)。click()$(​​“。rightside”)。click()绑定一次,一次又一次单击则绑定一次。在$(“。leftside”)和$(“。rightside”)上创建了另一个绑定,依此类推。

You can always unbind it before you bind it again. 您始终可以先解除绑定,然后再重新绑定。

$(".leftside").unbind('click').click(function (e) {
    // your code
}

Fiddle 小提琴

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

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