2014-03-19 148 views

回答

0

我在這裏更新您的提琴:http://jsfiddle.net/j45Lf/

更新使用點擊文件準備處理程序,然後使用數據屬性知道顯示/隱藏的頁面。

JS:

$(document).ready(function(){ 
    $(".navlist a").click(function(e){ 
     $(".page").hide(); 

     $("#" + $(this).data("page")).show(); 
    });   
}); 

HTML:

<ul class="navlist"> 
    <li class="litem" name="home"> 
     <a data-page="body_home" href="#body_home">Home</a> 
    </li> 
    <li class="litem" name="hello"> 
     <a data-page="body_hello" href="#body_hello">Hello</a> 
    </li> 
</ul> 

<div id="body_home" class="page" style="display:none;> 
    ... 
</div> 

<div id="body_hello" class="page" style="display: none;"> 
    ... 
</div> 
+0

這工作謝謝反正的製作環節的硬鏈接到正確的頁面(如的index.php),如果用戶已禁用JavaScript – Julian

0

如果你還沒有注意到這一點尚未$(document).ready()只在頁面加載或刷新頁面解僱。點擊<a>標籤,只向同一頁面上的URL添加片段不會重新加載頁面本身。

因此,您還需要另一個處理程序用於<a>點擊click()事件。

$('.litem a').click(function() { 

    ShowPage($(this).attr('href').substring(1)) 
}); 

function ShowPage(a) { 
    $(".page").css("display", "none");//.hide() will do 
    //don't need to set display, fadeIn already does that 
    $("#body_" + a).fadeIn(1000)//.css("display", "block"); 
    //window.location.hash = "#" + a;, it's not necessary to reset the hash 
    $(".litem").attr("id", ""); 
    $("li[name=" + a + "]").attr("id", "selected") 
} 
+0

謝謝你,有沒有無論如何,如果用戶禁用Javascript,則將鏈接設置爲正確頁面的硬鏈接(例如,index.php) ? – Julian

0

你也可以使用.fadeIn()有一個更漂亮,然後簡單地顯示。

$(文件)。就緒(函數(){$ ( 「navlist一 」)。點擊(函數(E){ $(「 頁」)。隱藏( );!?

$("#" + $(this).data("page")).fadeIn(); 
});   });