简体   繁体   English

如何只注册一次点击事件?

[英]How to register click event only once?

I am trying to register click event once to my element in my project. 我试图将click事件一次注册到项目中的元素。

I have simplfy my codes and basically my structure looks like the following: 我的代码简略,基本上我的结构如下所示:

function objectParent(){

}

objectParent.prototype.click=function()(

   $('#create').click(function(){
     var var1=new objectchild();
         var1.add();
   })

}

function objectchild(){

}

objectchild.prototype.add=function()(

  //The '#aButton' click event will be registered 4 times if the '#create' button is clicked 4 times.
  //so the alert will pop 4 times when 'aButton' is clicked which is not what I wanted.

  //I can use $('#aButton').unbind('click') to remove the click event but I have many buttons in my project
  //and I want to have better solution for this.

  $('#aButton').click(function(){
     alert('click')
   })

}

var test=new objectParent()
    test.click();

html html

<a id='create' href='#'>test</a>

//everytime this <a> is clicked, it will create a new objectchild object 
//after click 4 times of this button and click 'aButton', it will pop the alert box 4 times
//I know it register click event 4 times to the 'aButton' 
//I only want to pop the alert box once.

//I can use $('#aButton').unbind('click); to remove the click event but I have many many buttons in my codes
//ex bButton, cButton...etc. I want to have a better approach for my codes.

<a id='aButton' href='#'>button</a>

I hope I explain my problem well. 我希望我能很好地解释我的问题。 Thanks for the help! 谢谢您的帮助!

Just do: 做就是了:

$('#aButton').one('click', function(){
     alert('click')
});

Try: 尝试:

var clickHandler = function(event) {
  $('#element').unbind('click', clickHandler);
  ...
} 
$('#element').on('click', clickHandler);

http://jsfiddle.net/zYGhz/ http://jsfiddle.net/zYGhz/

I think Nelson & alex must be misunderstood what FlyingCat was asking... 我认为纳尔逊(Nelson)和亚历克斯(Alex)必须误解了飞猫(FlightCat)的要求...
the one() method can only make sure the registered handler be called only once, one()方法只能确保已注册的处理程序仅被调用一次,
but if the developer invoke one() method several times(just like in FlyingCat's scenario), 但是如果开发人员多次调用one()方法(就像在FlyingCat的场景中一样),

 for (var i = 0; i < 3; i++) {
     $("#element").one(function() {
          alert(1);
     })
 }

each registered handler will be called once when clicked, so problem still not resolved. 单击每个注册的处理程序一次,因此问题仍未解决。
Kolink's answer is correct, and minor changes you have to make. Kolink的答案是正确的,您必须进行一些小的更改。

The easiest approach by far is to ensure that any previous attachments of objectchild's click function are removed before attaching a new one. 到目前为止,最简单的方法是确保在附加新的objectchild的click功能之前,将其删除。 There are other approaches but the code would be more voluminous. 还有其他方法,但是代码会更多。

objectchild.prototype.add = function(){
    $('#aButton').off('click.x').on('click.x'function(){
        alert('click');
    });
};

.x is used to namespace the event. .x用于命名事件的名称空间。 This allows .off('click.x') to operate without affecting other click handlers that may have been attached elsewhere in your code. 这允许.off('click.x')进行操作而不会影响代码中其他位置可能附加的其他单击处理程序。 If no other click handlers are attached then namespacing is not strictly necessary, but it does no harm. 如果没有附加其他单击处理程序,则严格不需要命名空间,但这不会造成任何危害。

You will probably want to replace .x with something more meaningful. 您可能希望将.x替换为更有意义的内容。

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

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