简体   繁体   English

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

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

I am trying to generate a div element in the corner of the screen from a bookmarklet.我正在尝试从小书签在屏幕一角生成一个 div 元素。

I tried我试过了

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

to now avail.现在可以使用。

Thanks.谢谢。

You need to provide px with the values, and you got a typo in heigth :您需要为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);

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

This is mostly accurate, but you have a typo in height and you need to provide units for width , height and top .这大部分是准确的,但是您的height有误,您需要提供widthheighttop的单位。 I'm assuming pixels, but the interpreter won't be so kind;我假设像素,但解释器不会那么好; 200 could be in ems, pixels, points, percents ( 200em , 200px , 200pt and 200% , respectively): 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