在Bind而不是Click上执行的函数

时间:2022-03-18 20:24:53

Problem: A function I am binding with a click event is firing off on bind.

问题:我与click事件绑定的函数在绑定时触发。

I've found others with this same problem and found a couple of answers. However, on implementing those solutions, I am still seeing the same issue. I must be doing something wrong, but I can't seem to figure it out.

我发现其他人有同样的问题,并找到了几个答案。但是,在实施这些解决方案时,我仍然看到同样的问题。我一定做错了什么,但我似乎无法弄明白。

Demo of issue: https://jsfiddle.net/b35krwfh/16/

演示演示:https://jsfiddle.net/b35krwfh/16/

Original snippet of code (coffeescript) that was calling the function on bind:

在bind上调用函数的原始代码片段(coffeescript):

$("body").bind "click", deviceCloseView

After some online research, I found the below as the recommended fix, but I'm still experiencing the same issue:

经过一些在线研究后,我发现以下是推荐的修复方法,但我仍然遇到同样的问题:

$("body").bind "click", ->
  deviceCloseView()

Update: it was suggested I remove the parenthesis, but that doesn't seem to fix the issue as it isn't executing at all when I do that:

更新:有人建议我删除括号,但这似乎没有解决问题,因为当我这样做时它根本没有执行:

$("body").bind "click", ->
  deviceCloseView

https://jsfiddle.net/b35krwfh/15/

Some references I used trying to fix this:

我用过的一些引用试图解决这个问题:

jQuery function called in a .bind click handler is running on document ready

在.bind单击处理程序中调用的jQuery函数正在文档就绪上运行

Why does click event handler fire immediately upon page load?

为什么在页面加载时会立即触发click事件处理程序?

2 个解决方案

#1


1  

The deviceOpenView in your jsfiddle does not stop propagation of the click event. As you bind deviceCloseView to the body, it will get triggered by the same click as well.

jsfiddle中的deviceOpenView不会停止click事件的传播。将deviceCloseView绑定到正文时,它也会被同一个点击触发。

You need to change deviceOpenView to stop the propagation of the click event to prevent this.

您需要更改deviceOpenView以停止click事件的传播以防止这种情况发生。

deviceOpenView = (event) ->
  event.stopPropagation();
  // ... your other code

#2


1  

Your event is bubbling. Here is a fixed fiddle: https://jsfiddle.net/6nk3asfw/

你的活动正在冒泡。这是一个固定的小提琴:https://jsfiddle.net/6nk3asfw/

Call preventDefault, stopPropagation, return false, I just did all of them :)

调用preventDefault,stopPropagation,返回false,我只是做了所有这些:)

Some code:

deviceOpenView = (e) ->
    console.log("open");
    console.dir(e);
    e.preventDefault();
    e.stopPropagation();

#1


1  

The deviceOpenView in your jsfiddle does not stop propagation of the click event. As you bind deviceCloseView to the body, it will get triggered by the same click as well.

jsfiddle中的deviceOpenView不会停止click事件的传播。将deviceCloseView绑定到正文时,它也会被同一个点击触发。

You need to change deviceOpenView to stop the propagation of the click event to prevent this.

您需要更改deviceOpenView以停止click事件的传播以防止这种情况发生。

deviceOpenView = (event) ->
  event.stopPropagation();
  // ... your other code

#2


1  

Your event is bubbling. Here is a fixed fiddle: https://jsfiddle.net/6nk3asfw/

你的活动正在冒泡。这是一个固定的小提琴:https://jsfiddle.net/6nk3asfw/

Call preventDefault, stopPropagation, return false, I just did all of them :)

调用preventDefault,stopPropagation,返回false,我只是做了所有这些:)

Some code:

deviceOpenView = (e) ->
    console.log("open");
    console.dir(e);
    e.preventDefault();
    e.stopPropagation();