2012-11-02 42 views
0

我想爲單個網頁網站創建自動生成的導航菜單。自動生成單頁網站的導航菜單

<!-- html --> 
<section id="home"> 
</section> 
<section id="aboutus"> 
</section> 
<section id="contactus"> 
</section> 
<nav id="nav_menu"> 
</nav> 

// jQuery 
<script> 
    $(function() { 
    $('section').each(function(index) { 
     $('#nav_menu').append('<a href="'+$(this).hash+'">&diams;<br /></a>') 
    }); 
    }); 
</script> 

我想爲頁面中的每個部分創建一個菱形,其中每個菱形都有一個錨點以顯示相應的頁面。

問題是$(this).location.hash顯示undefined而不是頁面的錨點。我不知道應該使用什麼來獲取該部分的散列。

+0

的ID'this'指DOM元素不'window'在這種情況下,加'hash'是這是一個屬性location'的'的屬性'窗口'。 – elclanrs

+0

你會清除更多?寫你想要的部分 – diEcho

+0

你想在「$(this).location.hash」你想要在那裏的散列 – Jai

回答

0

你想要的部分

$('section').each(function(index) { 
    $('#nav_menu').append('<a href="#'+this.id+'">&diams;<br /></a>') 
}); 
+0

很好的回答.... :) – Jai