1
<html>
<head>
<style>

#wrapper{
position: absolute;
   top : 250px;
    width: 500px;
    height: 500px;
    border: 1px solid red;
}


.tagging{
position: absolute;  
   border: 1px solid black;
   width : 20px;
   height: 30px;


}


</style>
<script>
window.onload = function(event){
     var wrapper =  document.getElementById("wrapper");

     var wrapperOffsetTop = wrapper.offsetTop;


     wrapper.onmousedown = function(event){

          var div = document.createElement("div");
          div.className = "tagging";
         div.style.top = event.clientY  - wrapperOffsetTop;
         wrapper.appendChild(div);

     }
}
</script>

</head>
<body>
<div id="wrapper">

</div>
</body>
</html>

这是我的代码。我希望制作一个用户可以单击该区域的应用程序,它会在鼠标光标的位置附加一个 div 框,但它似乎不起作用。为什么??

添加后,这对我来说非常有用+"px"

但为什么在我的实际应用程序中它不起作用:

<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>
4

2 回答 2

5

您正在将 top 属性设置为Number。它需要一个 CSS 长度,它需要单位。

div.style.top = (event.clientY - wrapperOffsetTop) + "px";
于 2013-09-05T09:53:25.923 回答
2

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

除非我误解了你的问题,否则它应该只是为左边距的偏移量添加一个偏移量的情况:

<html>
<head>
<style>

#wrapper{
position: absolute;
   top : 250px;
    width: 500px;
    height: 500px;
    border: 1px solid red;
}


.tagging{
position: absolute;  
   border: 1px solid black;
   width : 20px;
   height: 30px;


}


</style>
<script>
window.onload = function(event){
     var wrapper =  document.getElementById("wrapper");

     var wrapperOffsetTop = wrapper.offsetTop;
     var wrapperOffsetLeft = wrapper.offsetLeft;


     wrapper.onmousedown = function(event){

          if (!event) {
              event = window.event;
          }
          var div = document.createElement("div");
          div.className = "tagging";
         div.style.top = event.clientY  - wrapperOffsetTop;
         div.style.left = event.clientX  - wrapperOffsetLeft;
         wrapper.appendChild(div);

     }
}
</script>

</head>
<body>
<div id="wrapper">

</div>
</body>
</html>
于 2013-09-05T09:59:22.363 回答