简体   繁体   English

JQuery event.stopPropagation()不起作用

[英]JQuery event.stopPropagation() not working

In my html I have a span of class dragHandle embedded within a li. 在我的html中,我有一个嵌入在li中的类dragHandle。

<div class='treeView'>
    <ul class='tree'>
        <li><span class="dragHandle"></span>Item 1
            <ul>
                <li><span class="dragHandle"></span>Item 2 <a href="#">link</a></li>
            </ul>   
        </li>
</ul>

I attach event handlers using jQuery as follows: 我使用jQuery附加事件处理程序如下:

$(".tree li").click(function(event) {
    alert("click");
    event.stopPropagation();
});

$(".dragHandle").mousedown(function(event) {
    alert("down");
    event.stopPropagation();

});

$(".dragHandle").mouseup(function(event) {
    alert("Up");
    event.stopPropagation();

});

When I mousedown and mouse up over the element I get the down and up alerts, however I also get the click alert of the li's event handler too. 当我mousedown并将鼠标放在元素上时,我得到了向下和向上警报,但是我也得到了li的事件处理程序的点击警报。 I thought that this should be prevented from by the call to event.stopPropagation in the mousedown and mouseup handlers. 我认为这应该通过调用mousedown和mouseup处理程序中的event.stopPropagation来防止。 How do I stop the click event being called for mousedown/up events on the dragHandle? 如何在dragHandle上停止为mousedown / up事件调用的click事件?

TIA, Adam TIA,亚当

How do I stop the click event being called for mousedown/up events on the dragHandle? 如何在dragHandle上停止为mousedown / up事件调用的click事件?

You capture... and eat... that event: 你捕捉......然后吃...... 那个事件:

$(".dragHandle").click(function(event) { event.stopPropagation(); });

The key here is that click , mousedown , and mouseup are distinct events. 这里的关键是clickmousedownmouseup是不同的事件。 Although you might think of a click as being a mousedown followed by a mouseup , in reality you might have click events triggered by user actions that don't even involve the mouse, as well as combinations of mousedown and mouseup that don't result in any click events at all. 虽然你可能会认为click是一个mousedown后跟一个mouseup ,但实际上你可能有用户操作触发的click事件甚至不涉及鼠标,以及mousedownmouseup组合不会导致任何click事件都可以。

You could create a simple wrapper-"class", that keeps track of mouse-down and up events: 您可以创建一个简单的包装器 - “类”,它可以跟踪鼠标按下和向上事件:

(function () {
   var DragDropHandler = {
      isDrag: false,

      mouseDownHandler: function (event) {
        alert("down");
        event.stopPropagation();
        this.isDrag = true;
      },

      mouseUpHandler: function (event) {
        alert("Up");
        event.stopPropagation();
        this.isDrag = false;
      },

      clickHandler: function (event) {
         event.stopPropagation();
         // Check if we've already received a mouseDown-event. If we have,
         // disregard the click event since we want drag-functionality
         if(this.isDrag) { return; }
         alert("click");
      }
   };

   $(".tree li").click(function(event) {
      DragDropHandler.clickHandler.call(DragDropHandler, event);
   });
   $(".dragHandle").mousedown(function(event) {
      DragDropHandler.mouseDownHandler.call(DragDropHandler, event);
   });
   $(".dragHandle").mouseup(function(event) {
      DragDropHandler.mouseUpHandler.call(DragDropHandler, event);
   });
})();

This creates a closure and delegates the event handling to the DragDropHandler-object. 这将创建一个闭包并将事件处理委托给DragDropHandler对象。 Note that I've used function.call (the first parameter is the context) to ensure that this refers to the DragDropHandler-object inside its methods. 请注意,我用function.call(第一个参数是上下文),以确保是指它的方法内DragDropHandler对象。 Since we have created an anonymous function that can not be reached from global space, I think it's acceptable to use the DragDropHandler reference inside the wrapper event handlers. 由于我们创建了一个无法从全局空间到达的匿名函数,我认为在包装器事件处理程序中使用DragDropHandler引用是可以接受的。

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

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