0
我嘗試在單擊時用其內容創建導航欄。我的代碼看起來很蹩腳。有沒有辦法縮短我的代碼?使用bootstrap選項卡及其內容的最佳方式
E.g:當房間點擊,JQuery的增加active
類#nav-hotel
,並去除來自其他li
元件active
類。它還自己和其他人設置visibility
和display
。
HTML:
<ul class="nav nav-tabs">
<li id="nav-hotel" role="presentation"><a href="#tab-hotel">Rooms</a></li>
<li id="nav-flight" role="presentation"><a href="#tab-flight">Flights</a></li>
<li id="nav-restaurant" role="presentation"><a href="#tab-restaurant">Restaurants</a></li>
</ul>
<div class="tab-content">
<div id="tab-hotel">hotel</div>
<div id="tab-flight">flight</div>
<div id="tab-restaurant">restaurant</div>
</div>
JQuery的:
$("#nav-hotel").click(function() {
$("#nav-hotel").toggleClass("active");
$("#tab-hotel").css("display", "block");
$("#tab-hotel").css("visibility", "visible");
$("#nav-flight").removeClass("active");
$("#tab-flight").css("display", "none");
$("#tab-flight").css("visibility", "hidden");
$("#nav-restaurant").removeClass("active");
$("#tab-restaurant").css("display", "none");
$("#tab-restaurant").css("visibility", "hidden");
});
$("#nav-flight").click(function() {
$("#nav-flight").toggleClass("active");
$("#tab-flight").css("display", "block");
$("#tab-flight").css("visibility", "visible");
$("#nav-hotel").removeClass("active");
$("#tab-hotel").css("display", "none");
$("#tab-hotel").css("visibility", "hidden");
$("#nav-restaurant").removeClass("active");
$("#tab-restaurant").css("display", "none");
$("#tab-restaurant").css("visibility", "hidden");
});
$("#nav-restaurant").click(function() {
$("#nav-restaurant").toggleClass("active");
$("#tab-restaurant").css("display", "block");
$("#tab-restaurant").css("visibility", "visible");
$("#nav-flight").removeClass("active");
$("#tab-flight").css("display", "none");
$("#tab-flight").css("visibility", "hidden");
$("#nav-hotel").removeClass("active");
$("#tab-hotel").css("display", "none");
$("#tab-hotel").css("visibility", "hidden");
});
我不知道'data-toggle'。感謝您的幫助! – Eniss