此代码在我尝试时无效。
<!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>
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;
答案 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>
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;