我正在尝试显示多个图像。单击时,应显示不同的文本。
我尝试通过默认显示与图像 1 关联的文本来执行此操作,给其他图像/文本 adisplay: none;
并在用户单击图像时使用switch
语句更改它。但是,我似乎遗漏了一些东西。我错过了什么?
相关的 JavaScript:
$(document).ready(function(){
$("#list1 > li").click(function(e){
switch(e.target.id){
case "case1":
//change status & style menu
$("#nav1").addClass("active");
$("#nav2").removeClass("active");
$("#nav3").removeClass("active");
$("#nav4").removeClass("active");
//display selected division, hide others
$("div.test1").fadeIn();
$("div.test2").css("display", "none");
$("div.test3").css("display", "none");
$("div.test4").css("display", "none");
break;
case "case2":
//change status & style menu
$("#nav1").removeClass("active");
$("#nav2").addClass("active");
$("#nav3").removeClass("active");
$("#nav4").removeClass("active");
//display selected division, hide others
$("div.test2").fadeIn();
$("div.test1").css("display", "none");
$("div.test3").css("display", "none");
$("div.test4").css("display", "none");
break;
case "case3":
//change status & style menu
$("#nav1").removeClass("active");
$("#nav2").removeClass("active");
$("#nav3").addClass("active");
$("#nav4").removeClass("active");
//display selected division, hide others
$("div.test3").fadeIn();
$("div.test1").css("display", "none");
$("div.test2").css("display", "none");
$("div.test4").css("display", "none");
break;
case "case4":
//change status & style menu
$("#nav1").removeClass("active");
$("#nav2").removeClass("active");
$("#nav4").addClass("active");
$("#nav3").removeClass("active");
//display selected division, hide others
$("div.test3").fadeIn();
$("div.test1").css("display", "none");
$("div.test2").css("display", "none");
$("div.test4").css("display", "none");
break;
}
//alert(e.target.id);
return false;
});
});
相关HTML:
<div id="slider">
<ul id="list1">
<li id="nav1" class="active"><a href="#"><img src="test-img.jpg" style="width:156px;height:156px;"></a></li>
<li id="nav2"><a href="#"><img src="test-img.jpg" style="width:156px;height:156px;"></a></li>
<li id="nav3"><a href="#"><img src="test-img.jpg" style="width:156px;height:156px;"></a></li>
<li id="nav4"><a href="#"><img src="test-img.jpg" style="width:156px;height:156px;"></a></li>
</ul>
<div class="test1">
<h2>Test tab</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut </p>
<p>labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut </p>
<p>aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse </p>
<p>cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in </p>
<p>culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="test2">
<h2>Test tab2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut </p>
<p>labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut </p>
<p>aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse </p>
<p>cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in </p>
<p>culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="test3">
<h2>Test tab3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut </p>
<p>labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut </p>
<p>aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse </p>
<p>cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in </p>
<p>culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>