For example, let's say I have

例如,假设我有

$(element).on('click', function() {/*Some Stuff*/});

defined somewhere, then later I want to add more features to that same click event. If I do

定义了某个地方,然后我想要为相同的单击事件添加更多的特性。如果我做

$(element).on('click', function() {/*Other Stuff*/});

will "Other Stuff" overwrite "Some Stuff"?

“其他东西”会覆盖“一些东西”吗?

5 个解决方案

#1


22

The second listener won't override the first. jQuery event handlers are added cumulatively and execute in the order they were attached.

第二个监听器不会覆盖第一个监听器。jQuery事件处理程序是累积添加的,并按照其附加的顺序执行。

The thing that does effect whether one handler prevents the other running is how the function cancels the browser's default click handler (i.e. how you stop the browser following the link's url)

一个处理器是否阻止另一个处理器运行的影响是函数如何取消浏览器的默认点击处理器(例如,如何在链接的url之后停止浏览器)

If you use traditional method:

如果你使用传统的方法:

$(element).on('click', function() {

    /* Some Stuff, do your thing */ 

    return false; 
});

The handler above will stop the event itself and propagating to other handlers.

上面的处理程序将停止事件本身并传播到其他处理程序。

But you can stop the event action without stopping its propagation or the handler:

但您可以停止事件动作,而不停止其传播或处理程序:

$(element).on('click', function(e) {

    // prevent the default action at first, just to make it clear :)
    e.preventDefault(); 

    /* do your thing */

    return /* what you want */;
});

This will ensure that the default click action doesn't occur but that the event continues to other hanlders. jQuery event also has some other useful event propagation methods (e.g. Event.stopPropagation()) which are worth getting to know.

这将确保默认的单击操作不会发生,但事件会继续发生在其他的hanlders中。jQuery事件还具有一些其他有用的事件传播方法(例如,Event.stopPropagation()),这是值得了解的。

更多相关文章

  1. Web小练习-JavaScript事件的简单练习,监听鼠标的移动
  2. 掌握JavaScript中的事件监听
  3. 如何在单击按钮时将桌面应用程序导航到系统中设置的默认邮件提供
  4. 使用js更改抛出事件的输入值
  5. Angular 2快速入门 - 我的应用程序组件未加载
  6. 如何在详细信息标记的结束事件上添加CSS转换?
  7. 无法从按钮onclick事件ASP.NET 4调用Javascript函数
  8. js获取点击事件的位置,兼容主流浏览器
  9. 为什么jquery click事件在plunker中工作但在任何浏览器中都没有

随机推荐

  1. PHP之array_unique实现二维数组去重
  2. 通过实例解析PHP数据类型转换方法
  3. 详细解说三种PHP嵌套HTML的写法
  4. 谈谈PHP运算符"::"、"->"和"=>"的区别
  5. 十大你需要在PHP中避免的坑
  6. PHP之使用cURL实现Get和Post请求
  7. 三分钟带你了解PHP四大主流框架的优缺点
  8. 用 Composer 开源组件构建自己的 PHP 框
  9. 基于PHP实现短信验证码发送次数限制解析
  10. 分享PHP扫码登录原理及实现方法