簡體   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