“if”场景有效,但 else if 则无效。我试图通过单击 class="show-real-pixels"
来切换图像<div class="image not-hidden">
<img src="img/healthyout/WF-onboard.png">
</div>
<div class="image hidden">
<img src="img/healthyout/HF-onboard.png">
</div>
<div class="btn pull-right show-real-pixels">show real pixels</div>
$(".show-real-pixels").click(
function () {
if ($(".image").hasClass("hidden")) {
$(".image").removeClass("hidden").addClass("not-hidden");
}
else if ($(".image").hasClass("not-hidden")) {
$(".image").removeClass("not-hidden").addClass("hidden");
}
});
最佳答案
您可以使用.toggleClass()而不是 if else
$(".image").toggleClass("hidden not-hidden");
关于javascript - 如果工作但不工作如果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628411/