[英]Why Firefox event.offsetX and offsetY are undefined?
I just stumbled upon a bug in my software that happened only on Firefox. 我只是偶然发现我的软件中只出现在Firefox上的一个错误。 The reason was that the event didn't have offsetX and offsetY defined.
原因是该事件没有定义offsetX和offsetY。
I managed to fix it thanks to this . 由于这个原因,我设法解决了这个问题 。
Anyways, my question is not a programming help request. 无论如何,我的问题不是编程帮助请求。 I'm just curious why these properties are undefined in Firefox?
我只是好奇为什么这些属性在Firefox中未定义? What is the reason behind it?
它背后的原因是什么?
I did look through: DOM3 UIEvent Spec. 我确实看过: DOM3 UIEvent Spec。 , DOM3 MouseEvent Spec.
, DOM3 MouseEvent Spec。 and DOM2 MouseEvent Spec.
和DOM2 MouseEvent Spec。 .
。
It appears that neither of the properties are mentioned there, so, I suppose that's an unofficial property introduced in other browsers? 似乎这里没有提到任何属性,所以,我认为这是在其他浏览器中引入的非官方属性?
Although mentioned in the W3 specification , the offsetx
/ offsety
properties themselves are implemented inconsistently across browsers. 虽然在W3规范中提到过,
offsetx
/ offsety
属性本身在浏览器中实现不一致。
While supported in IE, Webkit browsers and Opera, they all function slightly different to the specifications requirements, except for IE - according to the "Calculating offsetX, offsetY" section here . 虽然在IE,Webkit浏览器和Opera中都受到支持,但它们的功能都略有不同,除了IE之外 - 根据此处的“计算offsetX,offsetY”部分 。
The properties aren't supported in Firefox at all - it appears to be a long-time bug that is yet to be resolved. Firefox中根本不支持这些属性 - 它似乎是一个尚未解决的长期错误 。
"I suppose that's an unofficial property introduced in other browsers?"
“我想这是在其他浏览器中引入的非官方属性?”
I believe it's an official property, that just hasn't been implemented in Firefox. 我相信它是一个官方财产,在Firefox中尚未实现。 If it was a non-official IE property, it wouldn't have been implemented in the Webkit/Opera browsers, mentioned in the W3 spec nor would Firefox actually be trying to implement it (check out the bug link above).
如果它是一个非官方的IE属性,它将不会在W3规范中提到的Webkit / Opera浏览器中实现,也不会实际尝试实现它(请查看上面的bug链接)。
offsetX and offsetY are inconsistent in Firefox so you can do it this way offsetX和offsetY在Firefox中不一致,所以你可以这样做
document.body.onclick = function(e) {
e = e || window.event;
var target = e.target || e.srcElement,
rect = target.getBoundingClientRect(),
offsetX = e.clientX - rect.left,
offsetY = e.clientY - rect.top;
console.log([offsetX, offsetY]);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.