禁用捕获阶段传播时,Javascript 单击事件会填充“目标”元素

问题描述

这个问题部分是好奇心,部分是表现查询

如果我在窗口元素的捕获阶段停止传播点击事件过程,为什么“event.target”属性被点击的子 div 填充?

在阅读了事件阶段(捕获、目标和冒泡)之后,我认为在捕获阶段完成之前不会识别目标元素。我了解并通过下面的代码见证了实际的事件处理程序已被阻止运行,但我担心浏览器已经确定在捕获阶段完成之前单击了哪个子元素。

我想知道识别这个目标元素的实际执行是什么样的,即它是否使用鼠标事件坐标和元素的高度、宽度、顶部、左侧进行某种hitBox检查?我探索这个的目标是减少浏览器的执行负载。我正在使用 pixiJS 创建一个游戏模板,并希望在我的自定义代码中处理所有鼠标和键盘事件。

我担心浏览器可能正在执行某种递归命中测试来确定目标元素,即使在捕获阶段事件传播在窗口对象处停止。我还没有决定如何实现我的 GUI,所以如果它是由 HTML 元素组成的,我不想在认情况下执行任何额外的检查。

我也明白,无论正在执行什么检查,它都可能是有效的,并且对性能的影响可能可以忽略不计。然而好奇心仍然驱使我提出这个问题。

<!DOCTYPE html>
<html>
    <body>
        <script>
            const one                   = document.createElement('div');
            one.id                      = 'one';        //id
            one.style.backgroundColor   = 'red';        //color
            one.style.height            = '50px';       //height
            one.style.width             = '50px';       //width
            one.style.top               = '25px';       //top
            one.style.left              = '25px';       //left
            one.style.position          = 'absolute';   //position
            one.style.zIndex            = 1;            //zIndex
            
            const two                   = document.createElement('div');
            two.id                      = 'two';        //id
            two.style.backgroundColor   = 'blue';       //color
            two.style.height            = '100px';      //height
            two.style.width             = '100px';      //width
            two.style.top               = '25px';       //top
            two.style.left              = '25px';       //left
            two.style.position          = 'absolute';   //position
            two.style.zIndex            = 1;            //zIndex
            
            const three                 = document.createElement('div');
            three.id                    = 'three';      //id
            three.style.backgroundColor = 'green';      //color
            three.style.height          = '150px';      //height
            three.style.width           = '150px';      //width
            three.style.top             = '0px';        //top
            three.style.left            = '0px';        //left
            three.style.position        = 'absolute';   //position
            three.style.zIndex          = 1;            //zIndex
            
            document.body.appendChild(three);
            three.appendChild(two);
            two.appendChild(one);
            
            function captureHandler(e) {
                let msg = (e.currentTarget.id == e.target.id)
                    ?`Target Phase\nCurrent: ${e.currentTarget.id}; Target: ${e.target.id}`
                    :`Capture Phase\nCurrent: ${e.currentTarget.id}; Target: ${e.target.id}`;
                alert(msg);
                e.stopPropagation()
            }
            function bubbleHandler(e) {
                let msg = (e.currentTarget.id == e.target.id)
                    ?`Target Phase\nCurrent: ${e.currentTarget.id}; Target: ${e.target.id}`
                    :`Bubble Phase\nCurrent: ${e.currentTarget.id}; Target: ${e.target.id}`;
                alert(msg);
                //e.stopPropagation()
            }
            
            window.addEventListener(    'click',captureHandler,{capture: true});
            window.addEventListener(    'click',bubbleHandler);
            one.addEventListener(       'click',{capture: true});
            one.addEventListener(       'click',bubbleHandler);
            two.addEventListener(       'click',{capture: true});
            two.addEventListener(       'click',bubbleHandler);
            three.addEventListener(     'click',{capture: true});
            three.addEventListener(     'click',bubbleHandler);
        </script>
    </body>
</html>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)