我正在做 Odin 專案并堅持鍛煉。
要求是我必須在 HTML 檔案中的容器中添加特定元素,但是在運行它時根本無法顯示以下部分:
a<div>帶有黑色邊框和粉紅色背景顏色,其中包含以下元素:
另一個<h1>寫著“我在一個 div 中” 一個<p>寫著“我也是!”
對此的提示:在<div>使用 createElement 創建之后,將<h1>and<p>添加到它之前,然后將其添加到容器中。
這是我的代碼https://codepen.io/mohd057/pen/KKQWdYV
const container = document.querySelector('#container');
const myP = document.createElement("p");
myP.style.color = "red";
myP.textContent = "Hey I'm red!";
const myH = document.createElement("h3")
myH.style.color = "blue";
myH.textContent = "I'm a blue h3!";
container.appendChild(myP);
container.appendChild(myH);
const myDiv = document.createElement("div");
myDiv.setAttribute('style', 'border: black; background: pink;');
const anotherH = document.createElement("h1");
anotherH.textContent = "I'm in a div";
const anotherP = document.createElement("p");
anotherP.textContent = "ME TOO!";
container.appendChild(myDiv);
這是練習的鏈接https://www.theodinproject.com/lessons/foundations-dom-manipulation-and-events(在問題 3、1 和 2 上苦苦掙扎很好)
很想知道我哪里出錯了。
uj5u.com熱心網友回復:
什么不見??了:
border需要一個邊框樣式的屬性來顯示- 背景的顏色屬性被呼叫
background-color,否則你必須像這樣定義它 - 你沒有在里面附加任何元素
myDiv
const container = document.querySelector('#container');
const myP = document.createElement("p");
myP.style.color = "red";
myP.textContent = "Hey I'm red!";
const myH = document.createElement("h3")
myH.style.color = "blue";
myH.textContent = "I'm a blue h3!";
container.appendChild(myP);
container.appendChild(myH);
const myDiv = document.createElement("div");
myDiv.setAttribute('style', 'border: black solid; background-color: pink;');
const anotherH = document.createElement("h1");
anotherH.textContent = "I'm in a div";
const anotherP = document.createElement("p");
anotherP.textContent = "ME TOO!";
myDiv.appendChild(anotherH);
myDiv.appendChild(anotherP);
container.appendChild(myDiv);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491252.html
標籤:javascript dom
