且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加show / hide到我的div标签

更新时间:2023-12-04 23:43:10

如果你给你的内容划分一个类,说内容,很容易将它们选为一个组并隐藏它们。同样,如果您为菜单链接指定一个类,则可以一次为所有菜单指定一个单击处理程序。所以:

If you give your content divs a class, say "content", it is easy to select them as a group and hide them. Similarly, if you give your menu links a class you can assign a click handler to all of them at once. So:

<a class="menu" href="#content1">Content 1</a>
<div class="content" id="content1">Some content here</div>
<!-- and so forth for your other links and divs -->

<script>
   $(function() {
       $("a.menu").click(function() {
          $("div.content").hide();
          $(this.href).show();
          return false;
       });
   });
</script>

请注意,如果脚本没有真正需要将代码包装在document.ready处理程序中块出现在有问题的元素之后,但我已经在这里完成了。

Note that you don't really need to wrap the code in a document.ready handler if the script block appears after the elements in question, but I've done so here for completeness.

我意识到上面的内容可能与你的html标记不符,但是因为你没有实际上提供你的html标记我不得不猜... ...

I realise the above may not correspond to your html markup, but since you didn't actually provide your html markup I had to guess...

如果这个答案中有任何你不理解的东西我建议你阅读一些jQuery教程,比如作为来自jQuery网站的这个;教程超出了***答案的范围......

If there's anything in this answer that you don't understand I suggest you read through some jQuery tutorials, such as this one from the jQuery website; a tutorial is beyond the scope of a *** answer...