前端天天问-子组件mousedown阻止冒泡,父组件是否会触发click事件

211 阅读1分钟

问:如果子组件mousedown事件阻止冒泡,父组件是否能监听到click事件?

自问自答:click事件是由mousedown和mouseup事件组成。因此子组件mousedown阻止冒泡,父元素肯定没有mousedown事件,那么也不会触发click事件。

实际:会触发click事件。

因此我有以下几点疑问:

  1. click事件是否由down和up组成。如果上述逻辑自然不成立,没有啥好纠结的。
  2. 如果是,为啥还有触发呢。

欢迎给位大佬给予解答!!!

<!DOCTYPE html>
<html>
  <body>
    <div style="width: 300px;height: 300px;background: red;position: relative;"onclick="click1()" onmousedown="mousedown1()" onmouseup="mouseup1()">
        <div style="position: relative;width: 200px;height: 200px;background: orange;" onclick="click()" onmousedown="mousedown(event)" onmouseup="mouseup(event)">
</div></div>
<script>
function click(){
    console.log('child click')
}
function mousedown(event){
    console.log('child mousedown');
    if(event.stopPropagation){
        event.stopPropagation();
    }
}
function mouseup(){
    console.log('child mouseup')
}
function click(){
    console.log('father click')
}
function mousedown(event){
    console.log('father mousedown');
}
function mouseup(){
    console.log('father mouseup')
}
</script>
</body>
</html>