单页网站自动生成的导航菜单



我想为单页网站创建自动生成的导航菜单。

<!-- 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 而不是页面的锚点。我不确定要使用该部分的哈希。

您想要该部分的ID

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

最新更新