JavaScript 鼠标位置位于元素上(代码不起作用)

发布于 2024-08-13 16:40:12 字数 1118 浏览 8 评论 0 原文

我想在元素内部显示鼠标位置。代码如下:




Mouse Events Example


 function GetMousePositionInElement(ev, element)
           {
               var osx = element.offsetX;
               var osy = element.offsetY;

               var bottom = osy + element.height();

               var x = ev.pageX - osx;
               var y = bottom - ev.pageY;

               return { x: x, y: y, y_fromTop: element.height() - y };
           }
    function handleEvent(oEvent) {
        var oTextbox = document.getElementById("txt1");
        var elem = document.getElementById("div1");
        var xp = GetMousePositionInElement(oEvent, elem).x;
        var yp = GetMousePositionInElement(oEvent, elem).y; 

        oTextbox.value += "\n x = " + xp + "y= " + yp;

    }



使用鼠标单击并双击红色方块。

div style="width: 100px; height: 100px; background-color: red" onmouseover="handleEvent(event)" id="div1"> /div

textarea id="txt1" rows="15" cols="50"> /文本区域>

代码有问题。鼠标位置不显示在 texArea 内。我必须进行哪些更改才能使代码正常工作? (当然,并不是所有的代码都显示出来,我删除了一些 < 和 > inoder 以向您展示代码的某些部分,否则不会显示,但代码语法是正确的,这不是问题)

谢谢。

I want to display the mouse position when its inside an element.. heres the code:





Mouse Events Example


 function GetMousePositionInElement(ev, element)
           {
               var osx = element.offsetX;
               var osy = element.offsetY;

               var bottom = osy + element.height();

               var x = ev.pageX - osx;
               var y = bottom - ev.pageY;

               return { x: x, y: y, y_fromTop: element.height() - y };
           }
    function handleEvent(oEvent) {
        var oTextbox = document.getElementById("txt1");
        var elem = document.getElementById("div1");
        var xp = GetMousePositionInElement(oEvent, elem).x;
        var yp = GetMousePositionInElement(oEvent, elem).y; 

        oTextbox.value += "\n x = " + xp + "y= " + yp;

    }



Use your mouse to click and double click the red square.

div style="width: 100px; height: 100px; background-color: red" onmouseover="handleEvent(event)" id="div1"> /div

textarea id="txt1" rows="15" cols="50"> /textarea>

There is a problem in the code. Mouse position is not displayed inside the texArea. What changes do i have to make for the code to work and work correctly? (of-cource not all of the code is displayed and i removed some of the < and > inoder to show you some parts of the code that are not displayed otherwise but the code syntax is correct, thats not the problem)

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疑心病 2024-08-20 16:40:12

var osy = element.offsetY;

没有像 offsetY 这样的属性。您可能会想到 offsetTop。但请注意 offsetLeft/ Top 值是相对于 offsetParent 而不是页面的。如果你想要页面相对坐标,你需要循环遍历 offsetParents,或者,因为你似乎包含 jQuery,所以调用它的 offset 函数就是这样做的:

var offset= $(element).offset();
var osx= offset[0], osy= offset[1];

var 底部 = osy + element.height();

element 是一个 DOM HTMLDivElement 对象,而不是 jQuery 包装对象,因此它没有 height() 方法。添加包装器:

var bottom= osy+$(element).height();

或使用等效的 DOM 方法:

var bottom= osy+element.offsetHeight;

var y = 底部 - ev.pageY;

注意 pageY 不是 DOM 事件标准的一部分,并且在 IE 中也不可用。您可以通过使用标准 clientY 属性并调整页面的 scrollTop 来计算它。同样,当您使用自己的方法绑定事件处理程序时,jQuery 会为您执行此操作,将 Firefox pageY 扩展添加到所有浏览器:

$('#div1').mouseover(handleEvent);

而不是内联 onmouseover=... 事件处理程序属性。

var osy = element.offsetY;

There's no such property as offsetY. You may be thinking of offsetTop. However note the offsetLeft/​Top values are relative to the offsetParent not the page. If you want page-relative co-ordinates you would need to loop through offsetParents, or, since you seem to be including jQuery, call its offset function that does just that:

var offset= $(element).offset();
var osx= offset[0], osy= offset[1];

var bottom = osy + element.height();

element is a DOM HTMLDivElement object, not a jQuery wrapper object, so it doesn't have the height() method. Either add the wrapper:

var bottom= osy+$(element).height();

or use the equivalent DOM method:

var bottom= osy+element.offsetHeight;

var y = bottom - ev.pageY;

Note pageY is not part of the DOM Events standard and also not available in IE. You can calculate it by using the standard clientY property and adjusting for the page's scrollTop. Again, jQuery does this for you, adding the Firefox pageY extension to all browsers, when you use its own methods to bind your event handler:

$('#div1').mouseover(handleEvent);

instead of the inline onmouseover=... event handler attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文