如果有任何帮助,我将不胜感激,我认为我将要编写的代码完全错误。我想在单击按钮时更改标题颜色,使其变为蓝色>单击>红色>单击>绿色。到目前为止,我只能实现 BLUE >click> GREEN。它永远不会经历三种颜色。是否有不同的方式来编写 if 语句?还是我不知道的其他方法可以让我进行一些有条件的 css 样式更改?
var mySwitch = document.getElementById('header').style.background;
if (mySwitch == "darkblue") {
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("header").style.background = "red";
});
}
else if (mySwitch == "red") {
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("header").style.background = "green";
});
}
else {
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("header").style.background = "darkblue";
});
}
* {
margin: 0;
padding: 0;
list-style: none;
font-family: sans-serif;
}
#header {
background: darkblue;
color: white;
display: flex;
justify-content: space-around;
height: 12vh;
}
#header h1 {
text-align: left;
margin-top: 30px;
}
.main-nav ul {
padding: 0px;
font-size: 1.5rem;
display: flex;
margin-top: 40px;
}
.main-nav a {
color: white;
padding: 20px;
text-decoration: none;
}
.main-nav a:hover {
color: rgb(255, 148, 148);
}
.hero-section {
background-image: url("https://stackoverflow.blog/wp-content/uploads/2018/01/BrutalLifeCycleJavascript.png");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
height: 80vh;
color: white;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
button {
background: black;
height: 40px;
width: 80px;
color: white;
margin-top: 20px;
animation: jumpbutton 1s ease-out infinite;
outline: none;
border-radius: 15px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
</head>
<body>
<header id="header">
<nav class="main-nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<div class="hero-section">
<h1>JAVASCRIPT</h1>
<p id="paragraph">This is a testing environment</p>
<button type="submit" id="myBtn"> CLICK ME </button>
</div>
<div class="about-box">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam, asperiores!</p>
</div>
<!-- CHANGE HEADER COLOR, BACKGROUND & HERO IMAGE-->
<!--- -->
<script src="main.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
最佳答案
为此你甚至不需要 jquery。因此,请使用纯 JavaScript。我还建议使用 ES6(const
和 let
而不是 var
等等...)。
不要在事件处理程序声明周围添加 if 语句,而是将它们放在其中(参见 const color
)。
另请注意,background
样式具有比颜色更多的值(例如,重复)。您可以只设置和检查 backgroundColor
,或者您可以按空格拆分 background
值并获取第一个条目(这是实际的颜色集)。
const header = document.getElementById('header')
document.getElementById("myBtn").addEventListener("click", () => {
const headerBg = header.style.background.split(" ")[0]
const color = headerBg == "red" ? "green" : headerBg == "darkblue" ? "red" : "darkblue"
changeBg(header, color)
})
const changeBg = (n, color) => {
n.style.background = color
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<header id="header">
<nav class="main-nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<div class="hero-section">
<h1>JAVASCRIPT</h1>
<p id="paragraph">This is a testing environment</p>
<button type="submit" id="myBtn"> CLICK ME </button>
</div>
<div class="about-box">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam, asperiores!</p>
</div>
</body>
</html>
关于javascript - 我的背景颜色没有随 javascript 改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51352291/