设置顶部和左侧CSS属性 [英] Setting top and left CSS attributes

查看:72
本文介绍了设置顶部和左侧CSS属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我无法使用以下JavaScript设置顶部和左侧 CSS属性。

For some reason I'm unable to set the "top" and "left" CSS attributes using the following JavaScript.

var div = document.createElement('div');
div.style.position = 'absolute';
div.style.top = 200;
div.style.left = 200;
document.body.appendChild(div);

使用Firebug我可以看到 div 位置设置为绝对 ,但将 top left 属性未设置!

Using Firebug I can see that the div gets the position set to "absolute" but the top and left attributes are not set!

这是用于Firefox 3.6。

This is for Firefox 3.6.

推荐答案

您的问题是 top left 属性需要一种计量单位,而不仅仅是裸数:

Your problem is that the top and left properties require a unit of measure, not just a bare number:

div.style.top = "200px";
div.style.left = "200px";

这篇关于设置顶部和左侧CSS属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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