document.onmousemove = function () {
            var x = window.event.clientX;
            var y = window.event.clientY;
            var imgdiv = document.getElementById("imgdiv");
            if (!imgdiv) {
                return;
            }
            imgdiv.style.left = x;
            imgdiv.style.top = y;
            alert(imgdiv.style.left); alert(imgdiv.style.top);
        }
图片是不会动的,x和y是有值的,可为什么alert出来是空的消息框,值没传过去?

解决方案 »

  1.   

    document.onmousemove = function () {
      var x = window.event.clientX;
      var y = window.event.clientY;
      var imgdiv = document.getElementById("imgdiv");
      if (!imgdiv) {
      return;
      }
      imgdiv.style.left = x+'px';
      imgdiv.style.top = y+'px';
      alert(imgdiv.style.left); alert(imgdiv.style.top);
      }
    还有就是你图片样式写position:absolute;了吗?修改地方:看我标红的地方  加上'px'