我正在 try catch SVG 元素(在本例中为矩形)上触发的事件。但是,事件
无法识别,我收到控制台错误:
ReferenceError: event is not defined.
我在 HTML 元素上调用了相同的函数,并且运行良好。是否有任何原因导致以下内容不适用于 SVG 元素?
这是代码:
<g class="piece filled" transform="translate(120, 0)">
<!-- the following 'event' within 'playerMove' gives error -->
<rect width="120" height="120" data-tile="1" onclick="playerMove(event)"/>
</g>
Javascript:
const playerMove = function( e ){
console.log( e );
let eTarget = e.target;
}
编辑: 根据评论,这似乎是 Firefox 问题,有解决办法吗?它似乎可以在 Chrome 中运行。感谢 jessh 的提示。
最佳答案
将事件更改为 evt 以生成以下内容:
<rect width="120" height="120" data-tile="1" onclick="playerMove(evt)"/>
关于javascript - SVG 中的事件捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38885289/