为什么css中的绝对位置在javascript中不能正常工作

为什么css中的绝对位置在javascript中不能正常工作,javascript,css,Javascript,Css,您正在将top属性设置为数字。它需要CSS长度,这需要单位 <html> <head> <style> .tagging{ border: 1px solid red; position: absolute; padding: 0px; margin: 0px; width : 20px; height: 20px; } </style> <script> window.onlo

您正在将top属性设置为数字。它需要CSS长度,这需要单位

<html>
<head>
<style>
.tagging{
    border: 1px solid red;
    position: absolute; 
    padding: 0px;
    margin: 0px; 
    width : 20px;
    height: 20px;

}
</style>
<script>
  window.onload = function(){

      //get Information about imgWrapper
     var imgWrapper = document.getElementById("imgwrapper");
     var wrapperOffsetTop = imgWrapper.offsetTop;
     var wrapperOffsetLeft = imgWrapper.offsetLeft;


  //set the image wrapper to be the same size as the image
     var img = document.getElementsByTagName('img');
     imgWrapper.style.width = img[0].width;

     //when image wrapper is click, append div element
     img[0].onmousedown = function(event) {
       event.preventDefault();

       //create tag with class
       var div = document.createElement("div");
       div.className = "tagging";
        imgwrapper.appendChild(div);

        //get the position of mouse cursor
         var mouseX = event.clientX;
         var mouseY = event.clientY;

       //set the position of the div
        div.style.top =  (mouseY - wrapperOffsetTop) + "px";
        div.style.left  = (mouseX - wrapperOffsetLeft) + "px";  





         //mousemove event

        //assign mouse move action on image

        //mouse up



     }

  }
</script>
</head>
<body>
<div id="imgwrapper">
<img src="jlin.jpg">
</div>
<form action="./yipee.php" method="POST" id="noob">
<input type="submit">
</form>
</body>
</html>

您正在将top属性设置为数字。它需要CSS长度,这需要单位

<html>
<head>
<style>
.tagging{
    border: 1px solid red;
    position: absolute; 
    padding: 0px;
    margin: 0px; 
    width : 20px;
    height: 20px;

}
</style>
<script>
  window.onload = function(){

      //get Information about imgWrapper
     var imgWrapper = document.getElementById("imgwrapper");
     var wrapperOffsetTop = imgWrapper.offsetTop;
     var wrapperOffsetLeft = imgWrapper.offsetLeft;


  //set the image wrapper to be the same size as the image
     var img = document.getElementsByTagName('img');
     imgWrapper.style.width = img[0].width;

     //when image wrapper is click, append div element
     img[0].onmousedown = function(event) {
       event.preventDefault();

       //create tag with class
       var div = document.createElement("div");
       div.className = "tagging";
        imgwrapper.appendChild(div);

        //get the position of mouse cursor
         var mouseX = event.clientX;
         var mouseY = event.clientY;

       //set the position of the div
        div.style.top =  (mouseY - wrapperOffsetTop) + "px";
        div.style.left  = (mouseX - wrapperOffsetLeft) + "px";  





         //mousemove event

        //assign mouse move action on image

        //mouse up



     }

  }
</script>
</head>
<body>
<div id="imgwrapper">
<img src="jlin.jpg">
</div>
<form action="./yipee.php" method="POST" id="noob">
<input type="submit">
</form>
</body>
</html>

更新:最初的代码适用于Firefox和Chrome。要使它在IE8中工作,IE8不支持W3 DOM事件对象,您需要选择window.event 如果事件{ event=window.event; }

除非我误解了你的问题,否则这应该是一个简单的例子,即在左边距的偏移量中添加一个偏移量:

div.style.top = (event.clientY - wrapperOffsetTop) + "px";

更新:最初的代码适用于Firefox和Chrome。要使它在IE8中工作,IE8不支持W3 DOM事件对象,您需要选择window.event 如果事件{ event=window.event; }

除非我误解了你的问题,否则这应该是一个简单的例子,即在左边距的偏移量中添加一个偏移量:

div.style.top = (event.clientY - wrapperOffsetTop) + "px";

可能是因为传递了鼠标光标的坐标,你只是给它添加了一个类?可能是因为传递了鼠标光标的坐标,你只是给它添加了一个类?添加if!事件{event=window.event;}在首次使用该事件之前,将解析它。i、 e.在注释之前://获取鼠标光标的位置添加if!事件{event=window.event;}在首次使用该事件之前,将解析它。i、 e.在注释之前://获取鼠标光标的位置