2014-12-04 43 views
0

有人可以請解釋爲什麼下面的代碼,我簡單地移動一個div到鼠標點擊位置,只有當我刪除DOCTYPE標記時才工作?doctype和onclick事件

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Prova</title> 
    <style> 
     .bbox{ 
     width: 10px; 
     height:10px; 
     position:absolute; 
     background-color: orange; 
    } 
</style> 
</head> 
<body onclick = "moves()"> 
    <script> 
     function moves(){ 
     var cordx; 
     var cordy; 
     var d; 
     var e = window.event; 
     d= document.getElementById('box'); 
     cordx = e.clientX; 
     cordy = e.clientY; 
     d.style.left = cordx; 
     d.style.top = cordy; 

    } 
    </script> 



<div class="bbox" id='box'></div> 
</body> 
</html> 

回答

2

CSS要求lengths(除0)在其單位。

您正在將整數分配給d.style.leftd.style.top

如果你forget the Doctype那麼瀏覽器假設該網頁是寫在那個時代的瀏覽器的90年代和emulates the bugs。一旦此類錯誤將CSS中的整數視爲像素值而不是錯誤。

使用+ "px"