0
我正在使用單獨的.js
文件更改某些文本的顏色。我是HTML和JavaScript的新手,請詳細說明。謝謝。在html中使用javascript更改文本的顏色
這是我走到這一步:
htmlfile.html
<html>
<body>
<p id="demo"> Click the button to change the text in this paragraph. </p>
</body>
</html>
jsfile.js
var button = document.createElement("button")
button.innerHTML = "Red or green"
// Sets or returns the content of an element
// 2. Append somewhere
var body = document.getElementsByTagName("body")[0]
body.appendChild(button)
// Adds a new child node, to an element, as the last child node
// 3. Add event handler
button.addEventListener("click", function() {
state = !state
// Attaches an event handler to the specified element
//var led = document.createElement('LED')
if (state = 1) {
document.getElementById("demo").style.color = "red"
} else {
document.getElementById("demo").style.color = "green"
}
//body.appendChild(led)
})
}
的可能重複[使用JavaScript更改文本顏色?(http://stackoverflow.com/questions/17925577/change-text-color-with-javascript) – Santi