复制代码 代码如下://获取元素的位置 function getLeft(obj) { if (obj == null) return null; var mendingObj = obj; var mendingLeft = mendingObj.offsetLeft; while (mendingObj != null && mendingObj.offsetParent != null && mendingObj.offsetParent.tagName != "BODY") { mendingLeft = mendingLeft + mendingObj.offsetParent.offsetLeft; mendingObj = mendingObj.offsetParent; } return mendingLeft; }; function getTop(obj) { if (obj == null) return null; var mendingObj = obj; var mendingTop = mendingObj.offsetTop; while (mendingObj != null && mendingObj.offsetParent != null && mendingObj.offsetParent.tagName != "BODY") { mendingTop = mendingTop + mendingObj.offsetParent.offsetTop; mendingObj = mendingObj.offsetParent; } return mendingTop; }; //获取鼠标的位置 function getMousePosition(event) { var position = { MouseX: 0, MouseY: 0 } if (event.pageX != undefined) { position.MouseX = event.pageX; position.MouseY = event.pageY; } else { var target = EventUtil.getTarget(event); position.MouseX = event.offsetX + getLeft(target); position.MouseY = event.offsetY + getTop(target); } return position;
推荐阅读
js textarea自动增高并隐藏滚动条
复制代码 代码如下:<textarea id="tValue" style="overflow-y:hidden; height:20px;" onpropertychange="this.style.height=this.scrollHeight + 'px'" oninput="this.style.height=this.scrollHeight + 'px'"></t>>>详细阅读
本文标题:多浏览器兼容的获取元素和鼠标的位置的js代码
地址:http://www.17bianji.com/kaifa2/JS/27469.html
1/2 1