繁体   English   中英

页面加载后如何从javascript生成页面顶部的div?

[英]how to generate a div on the top of the page from javascript after the page is loaded?

我正在尝试从小书签在屏幕一角生成一个 div 元素。

我试过了

var newdiv = document.createElement('div');
newdiv.id="blabal";
newdiv.style.background = "#00C";
newdiv.style.border = "4px solid #000";
newdiv.style.width=300;
newdiv.style.heigth=200;
newdiv.style.position="fixed";
newdiv.style.top="20";
document.body.appendChild(newdiv);

现在可以使用。

谢谢。

您需要为px提供值,并且在heigth中有一个错字:

var newdiv = document.createElement('div');
newdiv.id="blabal";
newdiv.style.background = "#00C";
newdiv.style.border = "4px solid #000";
newdiv.style.width="300px";
newdiv.style.height="200px";
newdiv.style.position="fixed";
newdiv.style.top="20px";
document.body.appendChild(newdiv);

例如: http://jsfiddle.net/niklasvh/gy9XJ/

这大部分是准确的,但是您的height有误,您需要提供widthheighttop的单位。 我假设像素,但解释器不会那么好; 200可以是 em、像素、点、百分比(分别为200em200px200pt200% ):

var newdiv = document.createElement('div');
newdiv.id="blabal";
newdiv.style.background = "#00C";
newdiv.style.border = "4px solid #000";
newdiv.style.width="300px";
newdiv.style.height="200px";
newdiv.style.position="fixed";
newdiv.style.top="20px";
document.body.appendChild(newdiv);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM