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而不是页面的锚点。我不确定应该使用什么来获取该部分的哈希值。

4

1 回答 1

0

您想要该部分的 ID

$('section').each(function(index) {
  $('#nav_menu').append('<a href="#'+this.id+'">&diams;<br /></a>')
});
于 2012-11-02T05:09:52.957 回答