2010-11-18 49 views
2

爲什麼不能在此代碼中使用.style.top = "200px";?使用oclick="this.style.left='200px';"在元素內調用事件也不起作用。爲什麼不是obj.style.left =「200px」;在這個代碼中工作?

<html> 
    <head> 
    </head> 
    <body> 
     <div id="theDiv">The Div</div> 
     <button id="theButton">Do</button> 
    </body> 
</body> 
</html> 
<script type="text/javascript"> 
    document.getElementById("theButton").onclick = function(){ 
     document.getElementById("theDiv").style.top = "200px"; 
    }; 
</script> 
+1

什麼'position'的設置爲每個元素? – 2010-11-18 16:07:22

回答

7

要使用lefttop風格的元素必須是position:absolute,或position:relative,或position:fixed

+1

...或者'position:fixed;' – 2010-11-18 16:08:38

+0

@Matt Ball - 謝謝。 – 2010-11-18 16:09:12

相關問題