重新定位< div>用Javascript [英] Reposition <div> with Javascript

查看:30
本文介绍了重新定位< div>用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据特定条件在页面上重新放置div.

I am trying to reposition a div on the page depending on a certain condition.

if (somecondition)
    document.getElementById("Div1").setAttribute("style", "position: absolute; left:297px; top:882px; height: 30px; width: 181px; margin-top: 0px;");

它不会在下面的html中重新定位div:

It is not repositioning the div in the html below:

<div id="Div1" style="top:366px; left:134px; position:absolute; height: 30px; width: 175px;">

if触发时的其他代码.

Other code in the if fires.

任何人都可以帮忙吗?浏览器:IE8

Can anyone help? Browser: IE8

推荐答案

如果希望使更改反映在文档中,则使用setAttribute不可靠.改用Element.style:

Using setAttribute is unreliable if you want the change to be reflected in the document. Use Element.style instead:

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

这篇关于重新定位&lt; div&gt;用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆