【发布时间】:2023-01-08 02:17:46
【问题描述】:
我去了 chessboard.js https://chessboardjs.com/examples#5000 ,打开开发者工具并粘贴了下面的代码
document.body.addEventListener(`click`, a1);
function a1(){
console.dir(event.target);
}
当我点击一个空的国际象棋方格时,控制台打印出正确的结果(例如,“div#e5-b9f9-a5bc-69e1-3e5a-4b82-b2bc-ffe4-966c.square-55d63.black-3c85d.square- e5”)。
但是当我点击上面有棋子的方格时,控制台打印出“body”。当我右键单击同一个方块并选择“检查”时,它在开发人员工具的“元素”部分的“div”元素内正确显示了一个“img”元素(例如,<div class="square-55d63 white-1e1d7 square-e2" style="width:49px;height:49px;" id="e2-f699-d489-4d29-2e6f-2a64-c1ec-e26f-fb62" data-square="e2"><img src="chessboard/img/chesspieces/wikipedia/wP.png" alt="" class="piece-417db" data-piece="wP" style="width:49px;height:49px;"></div> ”)。
显示“正文”而不是正确元素的原因是什么?我应该怎么做才能让程序显示我点击的元素?
【问题讨论】:
-
可能有一个事件处理程序会停止对空方块的传播,但不会停止对已占用方块的传播。
-
无法重现。空 -> DIV,棋子 -> IMG -> minimal reproducible example
标签: javascript events event-handling dom-events