回答:
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ます。
あなたがそれをやっている方法に依存します。純粋な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');
乾杯!
divが追加されていないため、質問に回答していません
ここで他の答えは機能しますが、コンテンツを含む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; }
注:ラタールトマルから借りたCSS行
JSFiddle: https ://jsfiddle.net/Rani_Kheir/erL7aowz/
このソリューションはjqueryライブラリを使用します
$('#elementId').append("<div class='classname'>content</div>");
これが私が使用する1つの解決策です:
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;
やさしい。
[0]使用するときに指定する必要がありますgetElementsByTagName()。(編集するには短すぎます)
このように作成できます
board.style.cssText = "position:fixed;height:100px;width:100px;background:#ddd;"
document.getElementById("main").appendChild(board);
完全な実行可能なスニペット:
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);
<body>
<div id="main"></div>
</body>
ID名でdivを作成する
var 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
私がやりたいもう1つのことは、オブジェクトを作成してから、オブジェクト全体をループし、そのようにスタイルを設定することです。
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);