为什么单击按钮时以下代码不切换元素颜色?我的假设是 style.color 返回一个对象而不是字符串。纠正此问题的最佳方法是什么。先谢谢了!
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
asd=document.getElementById("demo")
if (asd.style.color!="rgb(255,0,0)")
asd.style.color="rgb(255,0,0)";
else
asd.style.color="rgb(0,0,0)";
}
</script>
</head>
<body>
<h1>My First JavaScript</h1>
<p id="demo">
JavaScript can change the style of an HTML element.
</p>
<button type="button" onclick="myFunction()">Click Me!</button>
</body>
</html>