File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed
docs/1-trial-session/13-dom Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -53,15 +53,16 @@ element.style.backgroundColor = "red";
5353
5454` document.createElement ` 関数は、引数に要素の種類を表す文字列を取り、その種類の新しい HTML 要素を作る関数です。
5555` document.createElement ` 関数の戻り値は、新しく作った HTML 要素に対応するオブジェクトです。
56+ 下の例では、新しい ` button ` 要素を作っています。
5657中身のない空の要素が作成されるので、` textContent ` を ` Hello World! ` に設定してみましょう。
5758
5859``` js title="script.js"
59- const newElement = document .createElement (" div " );
60- newElement .textContent = " Hello World!" ;
60+ const newButton = document .createElement (" button " );
61+ newButton .textContent = " Hello World!" ;
6162```
6263
6364そして、` 要素1.appendChild(要素2) ` とすることで、要素2を要素1の子要素に追加し、画面に表示することができます。
64- 今回は、div 要素の子要素にしてみましょう。
65+ 今回は、` div ` 要素の子要素にしてみましょう。
6566
6667``` html title="index.html"
6768<div id =" parent-element" ></div >
@@ -70,13 +71,13 @@ newElement.textContent = "Hello World!";
7071``` js title="script.js"
7172const parent = document .getElementById (" parent-element" );
7273
73- const newElement = document .createElement (" div " );
74- newElement .textContent = " Hello World!" ;
74+ const newButton = document .createElement (" button " );
75+ newButton .textContent = " Hello World!" ;
7576
76- parent .appendChild (newElement );
77+ parent .appendChild (newButton );
7778```
7879
79- これで、画面に ` Hello World! ` と表示されたはずです 。
80+ これで、既存の ` div ` 要素の子要素に新しい ` button ` 要素が追加され、 画面に ` Hello World! ` と書いてあるボタンが表示されます 。
8081
8182## 初級課題
8283
You can’t perform that action at this time.
0 commit comments