bind event only once

前端 未结 13 2262
予麋鹿
予麋鹿 2020-12-05 03:45

I have the following code:

function someMethod()
{
  $(obj).click(function {});
}

someMethod is called twice and thus click event is binded

13条回答
  •  死守一世寂寞
    2020-12-05 04:05

    In addition to pna's answer you may wish to think about namespacing your event so you do not go around unbinding all the click events accidentally.

    function someMethod() {
        $(obj).unbind('click.namespace').bind('click.namespace', function() { });
    }
    

    https://api.jquery.com/event.namespace/

提交回复
热议问题