“TypeError: Argument 1 of … is not a finite floating-point value” in Firefox

让人想犯罪 __ 提交于 2021-01-29 17:31:18

问题


I have a javascript code for clicking in mouse position (x/y)

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);
}

It works correctly in chrome but not in firefox . when i put this code in firefox console , it says : TypeError: Argument 1 of Document.elementFromPoint is not a finite floating-point value.

what's this error ? and how can i fix this error ?


回答1:


TypeError: Argument 1 of ... is not a finite floating-point value. means that the function expects a number and you passed it something else.

To fix, you need to find the line this error was reported on, and check what the first argument to elementFromPoint() is (in this error message the arguments are numbered starting from 1).

If it's indeed not a number, fix that and the error will go away.

In your snippet, the very first line uses cursorX/cursorY, which are not initialized. If you use the same code when you get the error, that could be the reason.




回答2:


Can you try this?

document.onmousemove = function(e){
    cursorX = e.x || e.clientX;
    cursorY = e.y || e.clientY;
    elem = document.elementFromPoint(cursorX, cursorY);
}



回答3:


e might not be defined/passed as a parameter in Firefox it's often when dealing with events that people will also have

e = e || window.event;

stated in their function. perhaps this will fix your problem



来源:https://stackoverflow.com/questions/35968401/typeerror-argument-1-of-is-not-a-finite-floating-point-value-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!