[英]Change background onclick twice
I have three boxes. 我有三个盒子。 When I click on any box, the corresponding
div
is displaying and the background color becomes red. 当我单击任何框时,将显示相应的
div
,并且背景颜色变为红色。 But when I click on the same box twice, the div
is disappearing, but the background stays the same (it should become white). 但是,当我两次单击同一框时,
div
消失了,但是背景保持不变(应该变成白色)。 Help me please. 请帮帮我。
Just a little thing: 只是一件事:
put this part at the end of your function show_hide_f()
: 将这部分放在函数
show_hide_f()
的末尾:
if (obj.style.display != "block") {
obj.style.display = "block";
} else {
obj.style.display = "none";
document.getElementById("webLI").style.background = "white";
document.getElementById("personalLI").style.background = "white";
document.getElementById("postLI").style.background = "white";
}
YOUR FULL JS UPDATED: 您的完整JS更新:
function show_hide_f(id) {
var obj = document.getElementById(id);
var objPortal = document.getElementById("web");
var objPersonal = document.getElementById("personal");
var objPost = document.getElementById("post");
if (obj == objPortal) {
document.getElementById("webLI").style.background = "red";
document.getElementById("personalLI").style.background = "white";
document.getElementById("postLI").style.background = "white";
objPersonal.style.display = "none";
objPost.style.display = "none";
} else if (obj == objPersonal) {
document.getElementById("webLI").style.background = "white";
document.getElementById("personalLI").style.background = "red";
document.getElementById("postLI").style.background = "white";
objPortal.style.display = "none";
objPost.style.display = "none";
} else if (obj == objPost) {
document.getElementById("postLI").style.background = "red";
document.getElementById("personalLI").style.background = "white";
document.getElementById("webLI").style.background = "white";
objPersonal.style.display = "none";
objPortal.style.display = "none";
}
if (obj.style.display != "block") {
obj.style.display = "block";
} else {
obj.style.display = "none";
document.getElementById("webLI").style.background = "white";
document.getElementById("personalLI").style.background = "white";
document.getElementById("postLI").style.background = "white";
}
}
See changes here too : http://jsfiddle.net/zh6hje9w/2/ 也可以在此处查看更改: http : //jsfiddle.net/zh6hje9w/2/
Easiest to do would be return false after your first else {} 最简单的方法是在您的第一个{}之后返回false
var obj = document.getElementById(id);
if (obj.style.display != "block") {
obj.style.display = "block";
} else {
obj.style.display = "none";
document.getElementById("webLI").style.background = "white";
document.getElementById("personalLI").style.background = "white";
document.getElementById("postLI").style.background = "white";
return false; // <-- here
}
You could use a small JQuery function, the first on the below code: 您可以使用一个小的JQuery函数,以下代码中的第一个:
$("li").on("dblclick", function () { $(this).css("background","white"); }); function show_hide_f(id) { var obj = document.getElementById(id); if (obj.style.display != "block") { obj.style.display = "block"; } else { obj.style.display = "none"; document.getElementById("webLI").style.background = "white"; document.getElementById("personalLI").style.background = "white"; document.getElementById("postLI").style.background = "white"; } var objPortal = document.getElementById("web"); var objPersonal = document.getElementById("personal"); var objPost = document.getElementById("post"); if (obj == objPortal) { document.getElementById("webLI").style.background = "red"; document.getElementById("personalLI").style.background = "white"; document.getElementById("postLI").style.background = "white"; objPersonal.style.display = "none"; objPost.style.display = "none"; } else if (obj == objPersonal) { document.getElementById("webLI").style.background = "white"; document.getElementById("personalLI").style.background = "red"; document.getElementById("postLI").style.background = "white"; objPortal.style.display = "none"; objPost.style.display = "none"; } else if (obj == objPost) { document.getElementById("postLI").style.background = "red"; document.getElementById("personalLI").style.background = "white"; document.getElementById("webLI").style.background = "white"; objPersonal.style.display = "none"; objPortal.style.display = "none"; } }
.MainMenu { position: relative; } .MainMenu ul { list-style-type: none; position: relative; } .MainMenu ul li { width: 100px; box-shadow: 4 4 10px rgba(0, 0, 0, 0.3); -moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.3); -webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.3); filter: progid:DXImageTransform.Microsoft.dropshadow(offX=10, offY=10, color=rgba(0, 0, 0, 0.3)); text-align: center; height: 45px; line-height:45px; margin-right: 10px; float: left; text-transform: uppercase; padding: 10px; background: white; cursor: pointer; position: relative; display: inline-flex; border: 1px solid grey; } .MainMenu ul li:hover { box-shadow: -4 -4 10px rgba(0, 0, 0, 0.3); -moz-box-shadow: -4px -4px 10px rgba(0, 0, 0, 0.3); -webkit-box-shadow: -4px -4px 10px rgba(0, 0, 0, 0.3); filter: progid:DXImageTransform.Microsoft.dropshadow(offX=10, offY=10, color=rgba(0, 0, 0, 0.3)); background: white; } .MainMenu ul li a { color: black !important; text-decoration: none; border: none; text-align: center; } .MainMenu ul li a:hover { }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="MainMenu"> <ul> <li id="personalLI"><a onclick="show_hide_f('personal')">Personal</a> </li> <li id="postLI"><a onclick="show_hide_f('post')">Post</a> </li> <li id="webLI"><a onclick="show_hide_f('web')">Web</a> </li> </ul> </div> <br/> <br/> <br/> <br/> <div id="web" style="display: none">Web</div> <div id="personal" style="display: none">Personal</div> <div id="post" style="display: none">Post</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.