我有一个用于单击鼠标位置 (x/y) 的 JavaScript 代码
var elem = document.elementFromPoint( cursorX,cursorY );
elem.addEventListener('click', function() {
console.log('clicked')
}, false);
var support = true;
try {
if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
support = false;
} else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
support = false;
}
} catch (e) {
support = false;
}
var cursorX;
var cursorY;
cursorX = 0; cursorY = 0;
document.onmousemove = function(e){
cursorX = e.clientX;
cursorY = e.clientY;
elem = document.elementFromPoint(e.clientX, e.clientY);
}
它在 chrome 中可以正常工作,但在 firefox 中却不能。 当我将此代码放入 Firefox 控制台时,它显示: TypeError: Document.elementFromPoint 的参数 1 不是有限浮点值。
这个错误是什么?我该如何修复这个错误?
最佳答案
TypeError: Argument 1 of ... is not a有限浮点值。
表示该函数需要一个数字,而您向它传递了其他内容。
要修复此问题,您需要找到报告此错误的行,并检查 elementFromPoint()
的第一个参数是什么(在此错误消息中,参数从 1 开始编号)。
如果它确实不是一个数字,请修复它,错误就会消失。
在您的代码片段中,第一行使用 cursorX
/cursorY
,它们未初始化。如果您在收到错误时使用相同的代码,则可能就是原因。
关于javascript - 火狐浏览器中的 "TypeError: Argument 1 of ... is not a finite floating-point value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35968401/