我是編程新手,很想了解我的代碼中發生了什麼,但與任何事情一樣,這需要時間。我知道這可能是明顯的與某人體驗,但我不知道如何做到這一點......我一直在使用達克特的JavaScript書籍作爲參考和指南。這個代碼是從他那裏借來的。需要指向隱藏的div
我需要能夠引用和顯示每個面板。我試過引用index.html#tab-1等。我不知道如何另外設置一個位置,以便它可以指向。多餘的方法是爲每個面板創建新的頁面,但我希望內容是動態的,並且文件結構是乾淨的。請幫忙。
HTML結構:
<ul class="tab-list">
<li class="active"><a class="tab-control" href="#tab-1">Mission & Vision</a></li>
<li><a class="tab-control" href="#tab-2">Facilities & Services</a></li>
<li><a class="tab-control" href="#tab-3">Policies</a></li>
<li><a class="tab-control" href="#tab-4">History</a></li>
</ul>
<div class="tab-panel active" id ="tab-1">
<p> Content etc. </p>
<div class="tab-panel active" id ="tab-2">
<p> Content etc. </p>
<div class="tab-panel active" id ="tab-3">
<p> Content etc. </p>
的Javascript:
$('.tab-list').each(function(){ // Find lists of tabs
var $this = $(this), // Store this list
$tab = $this.find('li.active'), // Get the active list item
$link = $tab.find('a'), // Get link from active tab
$panel = $($link.attr('href')); // Get active panel
$this.on('click', '.tab-control', function(e) { // When click on a tab
e.preventDefault(); // Prevent link behavior
var $link = $(this), // Store the current link
id = this.hash; // Get href of clicked tab
if (id && !$link.is('.active')) { // If not currently active
$panel.removeClass('active'); // Make panel inactive
$tab.removeClass('active'); // Make tab inactive
$panel = $(id).addClass('active'); // Make new panel active
$tab = $link.parent().addClass('active'); // Make new tab active
}
});
});
它可能只是我,但這聽起來像一個簡單的jQuery問題,似乎很多消化。你能否向我們展示不符合你期望的代碼行?具體來說,你想達到什麼目的?你的意思是'設置一個位置,以便它可以指向'? – 2014-11-24 15:42:34
它有點聽起來像你正在嘗試創建模板頁面?如果你想製作可以動態創建的「幻燈片」以及內容,那麼學習PHP會很好。 – vulpcod3z 2014-11-24 16:15:36
Php絕對是下一步。以下是我需要發生的事情: 1.用戶具有指向面板內容的鏈接。 index.html#tab-1 2.當用戶轉到鏈接tab-1時打開 由於阻止默認鏈接行爲,我無法通過它當前編碼的方式來實現此目的。但是,我不確定如何在不執行此操作的情況下讓選項卡正常工作。 – Poppe 2014-11-24 17:22:52