gpt4 book ai didi

javascript - 使用javascript在主要互联网浏览器中获取鼠标位置

转载 作者:行者123 更新时间:2023-11-29 10:25:15 25 4
gpt4 key购买 nike

我阅读了这篇关于创建 popup notes with javascript and css 的文章

问题是这个只能在 IE 中工作,因为 window.event 在 Firefox 中是未定义的。

// assigns X,Y mouse coordinates to note element
note.style.left=event.clientX;
note.style.top=event.clientY;

那么你能给我一个完整的例子吗?或者至少,我如何修改 javascript 代码以使其在两种 Internet 浏览器中都能正常工作?

最佳答案

有两个以上的浏览器,但以下内容应该适用于大多数浏览器(改编自您链接到的页面上的功能):

showNote = function(evt) {
evt = evt || window.event;
// gets note1 element
var note1=document.getElementById('note1');
// assigns X,Y mouse coordinates to 'note1' element
note1.style.left=evt.clientX;
note1.style.top=evt.clientY;
// makes note1 element visible
note1.style.visibility='visible';
};

问题是不是所有的浏览器都有 windowevent 属性,而是使用事件对象作为参数隐式传递给事件处理函数,例如 显示注释evt = evt || window.event; 行将 window.event 分配给 evt 变量,如果没有事件参数被传递到函数中(这是在 Internet Explorer 中发生的情况)。

关于javascript - 使用javascript在主要互联网浏览器中获取鼠标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com