2010-11-18 103 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

相关问题