我正在尝试根据特定条件在页面上重新定位一个 div。
if (somecondition)
document.getElementById("Div1").setAttribute("style", "position: absolute; left:297px; top:882px; height: 30px; width: 181px; margin-top: 0px;");
它没有重新定位下面 html 中的 div:
<div id="Div1" style="top:366px; left:134px; position:absolute; height: 30px; width: 175px;">
if 中的其他代码会触发。
有人可以帮忙吗? 浏览器:IE8
最佳答案
如果您希望更改反射(reflect)在文档中,则使用 setAttribute 是不可靠的。使用 Element.style 代替:
var el = document.getElementById('Div1');
el.style.position = 'absolute';
el.style.left = '397px';
el.style.top = '882px';
el.style.height = '30px';
el.style.width = '181px';
el.style.marginTop = '0px';
关于javascript - 使用 Javascript 重新定位 <div>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12602602/