如何使用javascript更改颜色样式?

时间:2016-09-18 09:19:10

标签: javascript html

此代码在我尝试时无效。

<!DOCTYPE html>
<html>
<body>
<h1 >My first java script</h1>
<p id="p2">I can change color by one click</p>
</br>
<button type="button" onclick="myFunction()">Click me</button>
<script>
    function myFunction() {
        document.getElementsById("p2").style.color = "yellow";
    }
</script>
</body>
</html>

&#13;
&#13;
function myFunction() {
  document.getElementsById("p2").style.color = "yellow";
}
&#13;
<h1>My first java script</h1>
<p id="p2">I can change color by one click</p>
</br>
<button type="button" onclick="myFunction()">Click me</button>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-2)

没有s的是getElementById。

<!DOCTYPE html>
<html>
<body>
<h1 >My first java script</h1>
<p id="p2">I can change color by one click</p>
</br>
<button type="button" onclick="myFunction()">Click me</button>
<script>
    function myFunction() {
        document.getElementById("p2").style.color = "yellow";
    }
</script>
</body>
</html>

&#13;
&#13;
function myFunction() {
  document.getElementById("p2").style.color = "yellow";
}
&#13;
<h1>My first java script</h1>
<p id="p2">I can change color by one click</p>
</br>
<button type="button" onclick="myFunction()">Click me</button>
&#13;
&#13;
&#13;