【问题标题】:How to avoid and return an event in jquery when you specify a new event?指定新事件时如何避免和返回jquery中的事件?
【发布时间】:2013-03-21 11:53:43
【问题描述】:

我需要覆盖事件,但是应用程序可以选择将原始事件返回到文档的每个元素。有什么想法吗?

例如:

    <div class="mydiv">
    </div>

    $(document).delegate( '.mydiv', click', function () { 
        alert('This is the original event my event!');
    });

    $(document).delegate( '.mydiv', 'click', function() {
       alert('Do this and ignore de previous...');
       // For example: How to return the original event when mouseleave event will executed?
    });

    $(document).delegate( '.mydiv', 'mouseleave', function() {
//       return the original event of mydiv 
    });

【问题讨论】:

  • 你为什么要首先将多个click 事件分配给同一个元素?

标签: javascript jquery events event-handling


【解决方案1】:

在创建new click 函数之前使用undelegate function

文档http://api.jquery.com/undelegate/

【讨论】:

    【解决方案2】:

    尝试检查函数中的点击事件

    $(document).delegate('.mydiv', click', function () {

        if(clicked){
              return;
          }
       alert('This is the original event my event!');
    
    });
    
    $(document).delegate( '.mydiv', 'click', function() {
           if(!clicked){
              return;
          }
       alert('Do this and ignore de previous...');
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多