0

当我尝试单击按钮上的任何一个选项时,有时我会单击一次(如预期的那样),有时我会使用 Stackoverflow 上某人给出的以下代码触发多次单击(不是我想要的)...

var $document = $(document);
var clickCount = 0;

var treeLogic = function (event) {
    // unbind the element from the event handler
    $document.off("click", "#file", treeLogic);

    clickCount = clickCount + 1; 
    // for testing the number of clicks on each click of a button. 
    // sometimes it shows one click and sometimes it shows more than one click
    // (not what I want).

    // bind the element back to the event handler
    $document.on("click", "#file", treeLogic);
};

$document.on("click", "#file", treeLogic);

HTML 代码

<input type='button' id='button'></input>

会不会是我的鼠标坏了?或者上面的逻辑不好,如果是这样,有人可以告诉我如何解决它吗?

4

1 回答 1

0

我删除了一些部分 .on 和 .off 绑定..这有效

Jsfiddled 在这里

html

<input type='button' id='file'></input>

javascript

var $document = $(document);
var clickCount = 0;
var treeLogic = function (event) {
    clickCount = clickCount + 1;
    console.log(clickCount);
};
$document.on("click", "#file", treeLogic);
于 2013-09-18T20:52:38.637 回答