【发布时间】:2020-08-25 09:24:19
【问题描述】:
点击按钮后,我使用 localStorage 将 div 的背景颜色更改为黄色。如果用户单击另一个按钮,旧按钮仍然具有黄色背景色。如果我再次点击它,它只会变成白色。
我想要实现的是只有被点击的按钮应该有黄色背景色,但我正在努力使用 localStorage 来实现它。
这是改变点击颜色的函数:
function selected(item) {
if(item.style.backgroundColor == 'yellow'){
// means the item is selected already. So unset it.
item.style.backgroundColor = 'white';
localStorage.removeItem(item.id);
}
else{
item.style.backgroundColor = 'yellow';
console.log(item.id);
localStorage.setItem(item.id, 'any value');
}
}
这是 div 的样子:
<div class="link" id="firstlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Ulm" style="background-color: white;">Ulm</div>
这是完整的代码:
<style>
#sideNavBox {display:none}
#contentBox {margin-left:0px}
#nav {
display: flex;
flex-wrap: wrap;
flex: 1 1 0px
}
.link {
max-width: 150px;
padding: 3px;
margin: 10px;
border: 2px solid lime;
border-radius: 15px;
flex-basis: 100%;
text-align: center;
cursor: pointer;
}
.active {
background-color: lime
}
.dd13:hover { cursor: pointer; }
.dd13 {
color: #FFFFFF;
Font: 12px Arial
background-color:: #48A040;
Padding: 3px 3px 3px 3px;
}
#pageStatusBar{
display:none!important;
}
</style><script>
window.addEventListener("load", function() {
document.getElementById("nav").addEventListener("click", function(e) {
if (e.target.classList.contains("link")) {
location = e.target.getAttribute("data-link");
}
})
})
var divItems = document.getElementsByClassName("link");
function selected(item) {
if(item.style.backgroundColor == 'yellow'){
// unset the item that is already selected
item.style.backgroundColor = 'white';
localStorage.removeItem(item.id);
}
else{
item.style.backgroundColor = 'yellow';
console.log(item.id);
localStorage.setItem(item.id, 'any value');
}
}
</script>
<h2>
<b>Seminare nach Standort filtern</b></h2>
<div id="nav">
<div class="link" id="firstlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Ulm" style="background-color: white;">Ulm</div>
<div class="link" id="secondlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Taufkirchen" style="background-color: white;">Taufkirchen<br/></div>
<div class="link" id="thirdlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Oberkochen" style="background-color: white;">Oberkochen</div>
<div class="link" id="fourthlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Köln" style="background-color: white;">Köln</div>
<div class="link" id="fifthlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Friedrichshafen" style="background-color: white;">Friedrichshafen</div>
<div class="link" id="sixthlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Wetzlar" style="background-color: white;">Wetzlar</div>
<div class="link" id="seventhlink" onclick="selected(this)" data-link="/internalseminars/SitePages/InternalSeminars.aspx?locations=Kiel" style="background-color: white;">Kiel<br/></div>
</div>
<div id="register">
<p>To register yourself to a seminar please click on this icon
<a title="Book for me" class="book-for-me-button"></a>. To register someone else to a seminar, please click on this icon
<a title="Book for me" class="book-for-user-button"></a>.<br/></p>
</div>
<script>
if(localStorage.getItem("firstlink")){
document.getElementById('firstlink').style.backgroundColor = "yellow";
}
if(localStorage.getItem("secondlink")){
document.getElementById('secondlink').style.backgroundColor = "yellow";
}
if(localStorage.getItem("thirdlink")){
document.getElementById('thirdlink').style.backgroundColor = "yellow";
}
if(localStorage.getItem("fourthlink")){
document.getElementById('fourthlink').style.backgroundColor = "yellow";
}
if(localStorage.getItem("fifthlink")){
document.getElementById('fifthlink').style.backgroundColor = "yellow";
}
if(localStorage.getItem("sixthlink")){
document.getElementById('sixthlink').style.backgroundColor = "yellow";
}
if(localStorage.getItem("seventhlink")){
document.getElementById('seventhlink').style.backgroundColor = "yellow";
}
</script>
我想过做这样的事情:
function selected(item) {
if(item.style.backgroundColor == 'yellow'){
// unset the item that is already selected
item.style.backgroundColor = 'white';
localStorage.removeItem(item.id);
} else if((document.getElementById("firstlink") has backgroundcolour ) || (document.getElementById("secondlink") has backgroundcolour)... ){
// remove backgroundColor
} else{
item.style.backgroundColor = 'yellow';
console.log(item.id);
localStorage.setItem(item.id, 'any value');
}
}
但这或多或少是伪代码,因为我不知道如何做到这一点。
感谢任何帮助!
【问题讨论】:
标签: javascript html button local-storage background-color