首页 > 阻止子元素触发父元素的不同类型的事件

阻止子元素触发父元素的不同类型的事件

以下代码,点击son的时候要求触发click事件,同时阻止father的mousedown事件。
在子元素中阻止事件冒泡不行,因为这里是不同的事件。
请问点击子元素,如何阻止父元素的不同类型的事件呢?

<div id="father"><span id="son"></span></div>

    $("#father").on("mousedown",function(){});
    $("#son").on("click",function(){});

暂时处理的一种方式是:

<div id="father"><span id="son"></span></div>

    $("#father").on("mousedown",function(){});
    $("#son").on("click",function(){});
    $("#son").on("mousedown",function(e){e.stopPropagation();});

但是感觉有点欠妥。


你那是一种解决办法,妥妥的。

还有一种

$("#father").on("mousemove", e => {
    if (e.target != e.currentTarget) {
        return;
    }
    console.log(e.target);
});

在son元素上添加一个mousedown,不冒泡不就行了吗?

【热门文章】
【热门文章】