var divTest = document.createElement("div");
divTest.setAttriubute("id","div1");
divTest.style.position="absolute";
divTest.top="";
divTest.left="";
divTest.width="";
divTest.width="";

解决方案 »

  1.   

    发个帖子,这么快就沉底儿
    1楼的代码可以简单实现,但是width和height都是不确定的,就是我怎么通过一个函数算出来他的高度和宽度
    我现在是onmousedown时用一个函数得到了他的起始坐标,鼠标松开时用一个函数记录下了他的坐标,但是两个函数之间怎么通用这个数据啊?
    另一种思路就是鼠标松开时用一个函数实现这个功能,那又该怎么弄呢?
    我想实现的效果就是模拟画层,就像用鼠标在页面上画出来一个层一样
      

  2.   

    <div style="border:1px solid #ff0000;width:500px;height:500px;cursor:hand;" onclick="test();"></div>
    <div style="border:1px solid #ff0000;width:500px;height:500px;cursor:hand;" onclick="test2();"></div><script>
    //指针在节点左上角位置
      function test(){
        var dd = document.createElement("div");
        dd.style.width = "100px";
        dd.style.height = "50px";
        dd.style.border = "1px solid #000000";
        dd.style.position = "absolute";
        dd.style.top = document.body.scrollTop + event.clientY + "px";
        dd.style.left = document.body.scrollLeft + event.clientX + "px";    
        document.body.appendChild(dd);
      }
      //指针在节点右下角位置
      function test2(){
        var dd = document.createElement("div");
        dd.style.width = "100px";
        dd.style.height = "50px";
        dd.style.border = "1px solid #000000";
        dd.style.position = "absolute";
        numWidth = dd.style.width.replace("px","");
        numHeight = dd.style.height.replace("px","");
        dd.style.top = document.body.scrollTop + event.clientY - numHeight + "px";
        dd.style.left = document.body.scrollLeft + event.clientX - numWidth + "px";
        
        document.body.appendChild(dd);
      }
    </script>
      

  3.   

    修改了一下 获取事件的问题 现在可以兼容火狐了 
    <head>
    <script>
    function mouseMove(ev){
    var ev= ev || window.event;
    var mousePos = mouseCoords(ev);
    var dd = document.createElement("div");
        dd.style.width = "100px";
        dd.style.height = "50px";
    dd.style.border = "1px solid #000000";
    dd.style.position = "absolute";
    dd.style.top = document.body.scrollTop + mousePos.x + "px";
    dd.style.left = document.body.scrollLeft + mousePos.y + "px";    
    document.body.appendChild(dd);
    }function mouseCoords(ev){
     if(ev.pageX || ev.pageY){
       return {x:ev.pageX, y:ev.pageY};
     }
     return {
         x:ev.clientX,y:ev.clientY
     };
    }
    </script>
    </head>
    <body>
    <div id="T" style="border:1px solid #ff0000;width:500px;height:500px;cursor:hand;background:#FF0000;" onclick="mouseMove(event);">OK</div>
    </body>
      

  4.   

    dd.style.top = document.body.scrollTop + mousePos.x + "px";
        dd.style.left = document.body.scrollLeft + mousePos.y + "px";    
    --〉x,y 互换一下