Javascript getBoundingClientRect 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48290490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getBoundingClientRect is not a function
提问by Sam
I am trying to get the coordinates of the mouse-click on a image. So I'm using getBoundingClientRect like this
我正在尝试获取鼠标单击图像的坐标。所以我像这样使用 getBoundingClientRect
function showCoords(canvas, event) {
var rect = canvas.getBoundingClientRect();
var x = event.clientX - rect.left;
var y = event.clientY - rect.top;
console.log("x: " + x + " y: " + y);
}
But I get this error "canvas.getBoundingClientRect is not a function"
但我收到此错误“canvas.getBoundingClientRect 不是函数”
采纳答案by Sergii Rudenko
That means that canvasvariable is not actually a Canvas element.
这意味着canvas变量实际上不是 Canvas 元素。
It can be undefined, still not initialized or incorrectly selected.
它可能undefined,仍然没有初始化或错误地选择。
You need to double check it and maybe try to use event.targetif the click event is added to the canvas element.
您需要仔细检查它,event.target如果单击事件添加到画布元素,可能会尝试使用。

