日期:2014-05-17  浏览次数:20746 次

DOM中的问题,为什么新建的<div>层的坐标不是鼠标的位置,求高手解释
<head>
    <title></title>
    <script type="text/javascript">
        function initEvent() {
            var as = document.getElementsByTagName("a");
            for (var i = 0; i < as.length; i++) {
                var a = as[i];
                a.onmouseover = aMouseOver;
            }
        }
        function aMouseOver() {
            //动态创建一个层;
            var div = document.createElement("div");
            div.style.position ="absolute";
            div.style.top = window.event.clientY;
            div.style.left = window.event.clientX;
            div.innerHTML = "测试";

            document.body.appendChild(div);//放到body中
        }
    </script>
</head>
<body onload="initEvent()">
<a href="http://www.baidu.com" >百度</a><br /><br />
<a href="http://www.baidu.com" >百度</a><br /><br />
<a href="http://www.baidu.com" >百度</a><br /><br />
</body>


为什么新建的<div>层的坐标不是鼠标的位置,求高手解释div.style.top = window.event.clientY;
            div.style.left = window.event.clientX;

------解决方案--------------------
Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.


用screenX,screenY试试看吧