绑定事件仅一次 [英] bind event only once

查看:81
本文介绍了绑定事件仅一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

someMethod被调用两次,因此click事件被绑定两次.如何使它只绑定一次?

someMethod is called twice and thus click event is binded twice. How can I make it bind only once?

推荐答案

如果可以应用它,可能想看看 event.preventDefault event.stopPropagation 或每次都在您的方法之内取消绑定并绑定,例如

If you can apply it, probably want to take a look at event.preventDefault and event.stopPropagation OR unbind and bind each time, within your method like

function someMethod()
{
  $(obj).off('click').on('click', function(e) {
    // put your logic in here 
  });
}

这篇关于绑定事件仅一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆