javascript - 为什么不是 obj.style.left = "200px";在这段代码中工作?

标签 javascript css dom-events

为什么 .style.top = "200px"; 不能在这段代码中工作?使用 oclick="this.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>

最佳答案

要使用 lefttop 样式,元素必须是 position:absoluteposition:relative , 或 position:fixed

关于javascript - 为什么不是 obj.style.left = "200px";在这段代码中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4216869/

相关文章:

javascript - 改变window.open的 Action

javascript - jQuery - 字段输入 - 如何复制并转换为 seo 友好的 url

javascript - JSON 与 JavaScript 对象数组。为什么第二种方法的结果与第一种方法不同?以及如何实现这一目标?

html - 如何将文件上传按钮放在幻灯片菜单中?

javascript - 每次用户按下javascript中的键时如何触发事件

多个元素上的 JavaScript 单击事件监听器并获取目标 'rel' 属性

javascript - Json 对象路径中的 Angular 2/4 插值

javascript - 使用 .map() 创建元素 - DOM 使用逗号构建它

javascript - 一次突出显示多个表格单元格

css - 媒体查询测试和设备 View 的问题