我不确定为什么单击“试用”按钮后出现的文本不是红色?
HTML:
<button onClick="myFunction()">Try it</button>
Javascript:
function myFunction(){
"use strict";
var newpara = document.createElement("p");
var newcontent = document.createTextNode("This is a paragraph.");
// [ADD] Ensure that content is added to the paragraph element
newpara.appendChild(newcontent);
// [ADD] Ensure the paragraph element is added to the document
document.body.appendChild(newpara);
Element.style.color="#ff3300";
}
答案 0 :(得分:0)
尝试
newpara.style.color = '#ff3300'
答案 1 :(得分:0)
将Element
正确地设为newpara
newpara.style.color="#ff3300";
function myFunction(){
"use strict";
var newpara = document.createElement("p");
var newcontent = document.createTextNode("This is a paragraph.");
// [ADD] Ensure that content is added to the paragraph element
newpara.appendChild(newcontent);
// [ADD] Ensure the paragraph element is added to the document
document.body.appendChild(newpara);
newpara.style.color="#ff3300";
}
<button onClick="myFunction()">Try it</button>
答案 2 :(得分:0)
执行newpara.style.color
。此外,您需要在newcontent
内附加newpara
function myFunction() {
"use strict";
var newpara = document.createElement("p");
newpara.style.color = "#ff3300";
var newcontent = document.createTextNode("This is a paragraph.");
newpara.appendChild(newcontent)
document.body.appendChild(newpara);
}
<button onClick="myFunction()">Try it</button>