我是 JS 新手,在使用简单脚本时遇到了麻烦。在我看来,当我按下按钮时,它应该在黑色和红色之间切换。但它只会变红然后保持这种状态。我确定我只是缺少一些基本知识。任何帮助,将不胜感激:
<!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<p id="demo">
JavaScript can change the style of an HTML element.
</p>
<script>
function myFunction()
{
x = document.getElementById("demo") // Find the element
if (x.style.color = "#000000")
{
x.style.color = "#ff0000";
}
else
{
x.style.color = "#000000";
}
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
</body>
</html>