admin管理员组

文章数量:1312662

Why wont .style.top = "200px"; work in this code? Calling the event within the element using oclick="this.style.left='200px';" doesn't work either.

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

Why wont .style.top = "200px"; work in this code? Calling the event within the element using oclick="this.style.left='200px';" doesn't work either.

<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>
Share Improve this question edited Jun 16, 2022 at 12:27 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 18, 2010 at 16:05 BabikerBabiker 18.8k28 gold badges82 silver badges127 bronze badges 1
  • 1 What are the position's set to for each element? – Evan Mulawski Commented Nov 18, 2010 at 16:07
Add a ment  | 

1 Answer 1

Reset to default 9

To use a left or top style the element must be position:absolute, or position:relative, or position:fixed

本文标签: javascriptWhy isn39t objstyleleftquot200pxquot working in this codeStack Overflow