如何正确使用“ Element.style.color”?

时间:2018-09-08 15:55:59

标签: javascript html element dom-manipulation

我不确定为什么单击“试用”按钮后出现的文本不是红色?

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";
}

3 个答案:

答案 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>