如何使用JavaScript创建和设置(并附加到页面)包含内容的div? 我知道这是可能的,但是怎么样?
答案 0 :(得分:247)
var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "white";
div.innerHTML = "Hello";
document.getElementById("main").appendChild(div);
<body>
<div id="main"></div>
</body>
var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "white";
div.innerHTML = "Hello";
document.getElementById("main").appendChild(div);
OR
document.body.appendChild(div);
使用父引用而不是document.body
。
答案 1 :(得分:55)
取决于你是如何做到的。纯javascript:
var div = document.createElement('div');
div.innerHTML = "my <b>new</b> skill - <large>DOM maniuplation!</large>";
// set style
div.style.color = 'red';
// better to use CSS though - just set class
div.setAttribute('class', 'myclass'); // and make sure myclass has some styles in css
document.body.appendChild(div);
使用jquery做同样的事情很容易:
$('body')
.append('my DOM manupulation skills dont seem like a big deal when using jquery')
.css('color', 'red').addClass('myclass');
干杯!
答案 2 :(得分:8)
此解决方案使用jquery库
$('#elementId').append("<div class='classname'>content</div>");
答案 3 :(得分:8)
虽然此处的其他答案有效,但我注意到您要求提供内容的div。所以这是我的版本与额外的内容。 JSFiddle链接在底部。
JavaScript (带评论) :
// Creating a div element
var divElement = document.createElement("Div");
divElement.id = "divID";
// Styling it
divElement.style.textAlign = "center";
divElement.style.fontWeight = "bold";
divElement.style.fontSize = "smaller";
divElement.style.paddingTop = "15px";
// Adding a paragraph to it
var paragraph = document.createElement("P");
var text = document.createTextNode("Another paragraph, yay! This one will be styled different from the rest since we styled the DIV we specifically created.");
paragraph.appendChild(text);
divElement.appendChild(paragraph);
// Adding a button, cause why not!
var button = document.createElement("Button");
var textForButton = document.createTextNode("Release the alert");
button.appendChild(textForButton);
button.addEventListener("click", function(){
alert("Hi!");
});
divElement.appendChild(button);
// Appending the div element to body
document.getElementsByTagName("body")[0].appendChild(divElement);
<强> HTML:强>
<body>
<h1>Title</h1>
<p>This is a paragraph. Well, kind of.</p>
</body>
<强> CSS:强>
h1 { color: #333333; font-family: 'Bitter', serif; font-size: 50px; font-weight: normal; line-height: 54px; margin: 0 0 54px; }
p { color: #333333; font-family: Georgia, serif; font-size: 18px; line-height: 28px; margin: 0 0 28px; }
注意:从Ratal Tomal借来的CSS行
答案 4 :(得分:4)
以下是我使用的解决方案之一:
var div = '<div id="yourId" class="yourClass" yourAttribute="yourAttributeValue">blah</div>';
如果您希望属性和/或属性值基于变量:
var id = "hello";
var classAttr = "class";
var div = '<div id='+id+' '+classAttr+'="world" >Blah</div>';
然后,附加到身体:
document.getElementsByTagName("body").innerHTML = div;
很容易就是馅饼。
答案 5 :(得分:2)
您可以像这样创建
board.style.cssText = "position:fixed;height:100px;width:100px;background:#ddd;"
document.getElementById("main").appendChild(board);
完整的Runnable片段:
var board;
board= document.createElement("div");
board.id = "mainBoard";
board.style.cssText = "position:fixed;height:100px;width:100px;background:#ddd;"
document.getElementById("main").appendChild(board);
&#13;
<body>
<div id="main"></div>
</body>
&#13;
答案 6 :(得分:1)
使用id名称
创建divvar divCreator=function (id){
newElement=document.createElement("div");
newNode=document.body.appendChild(newElement);
newNode.setAttribute("id",id);
}
将文字添加到div
var textAdder = function(id, text) {
target = document.getElementById(id)
target.appendChild(document.createTextNode(text));
}
测试代码
divCreator("div1");
textAdder("div1", "this is paragraph 1");
输出
this is paragraph 1
答案 7 :(得分:1)
这将在具有自定义CSS且类名称为Custom的函数或脚本标记内
var board = document.createElement('div');
board.className = "Custom";
board.innerHTML = "your data";
console.log(count);
document.getElementById('notification').appendChild(board);
答案 8 :(得分:0)
我想做的另一件事是创建一个对象,然后循环遍历该对象并设置类似的样式,因为这样可能会很乏味地逐个编写每个样式。
var bookStyles = {
color: "red",
backgroundColor: "blue",
height: "300px",
width: "200px"
};
let div = document.createElement("div");
for (let style in bookStyles) {
div.style[style] = bookStyles[style];
}
body.appendChild(div);
答案 9 :(得分:0)
您可以使用以下方法:
document.write()
很简单,我在下面的文档中解释
document.write("<div class='div'>Some content inside the div (It is styled!)</div>")
.div {
background-color: red;
padding: 5px;
color: #fff;
font-family: Arial;
cursor: pointer;
}
.div:hover {
background-color: blue;
padding: 10px;
}
.div:hover:before {
content: 'Hover! ';
}
.div:active {
background-color: green;
padding: 15px;
}
.div:active:after {
content: ' Active! or clicked...';
}
<p>Below or above well show the div</p>
<p>Try pointing hover it and clicking on it. Those are tha styles aplayed. The text and background color changes.</p>