使用 anchor 的 .hash
属性作为选择器来切换元素的 div 状态(显示、隐藏),例如:
$('#navbar a').click(function(e) {
$('.container > div').hide();
$(this.hash).show();
e.preventDefault(); //to prevent scrolling
});
和<div>
元素:
<div class="container">
<div id="section1">Section 1</div>
<div id="section2">Section 2</div>
<div id="section3">Section 3</div>
<div id="section4">Section 4</div>
</div>
它有效,但在页面首次加载时无效,当它首次加载时,所有 div 都会显示。
如何隐藏除第一个以外的所有div?
请看jsfiddle
最佳答案
关于javascript - 如何在单击加载时隐藏除 anchor href 引用的第一个 div 之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32957892/