javascript - 火狐浏览器中的 "TypeError: Argument 1 of ... is not a finite floating-point value"

标签 javascript firefox

我有一个用于单击鼠标位置 (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/

相关文章:

css - 伪元素未在左上角对齐

css - Firefox 中的缩放问题

javascript - 为什么javascript中的nextSibling方法不能正常工作

javascript - 在 Svelte 中,当在 radio 输入中使用 "bind:group"时,未设置 "checked"属性

javascript - 在后台上下文中的脚本之间进行通信(后台脚本,浏览器操作,页面操作,选项页面等)

javascript - 如何阻止我的网站被别人陷害?

javascript - 为什么 Firefox 从 CSS 填充值的 parseInt 返回 NaN?

html - CSS 文本背景剪辑在 Firefox 上与在 Chrome 上非常不同

firefox - 为多个 SDK 版本开发 Firefox 插件

javascript - 使用 for 循环 javascript 比较数组元素