为什么 .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>
最佳答案
要使用 left
或 top
样式,元素必须是 position:absolute
或 position:relative
, 或 position:fixed
关于javascript - 为什么不是 obj.style.left = "200px";在这段代码中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4216869/