jquery 中添加/删除类的问题
我正在尝试创建一个类似于选项卡但带有垂直按钮的菜单导航。当我启动页面时,第一个 li 类被删除,当我单击任何其他链接时,除了显示我的内容 div 之外,没有任何事情发生
。链接应始终在页面开始时处于活动状态。
<script type="text/javascript">
$(document).ready(function() {
var tabContainers = $('div.pages > div');
$('div.sidemenu ul.list a').click(function () {
tabContainers.hide().filter(this.hash).show();
$('div.sidemenu ul.list li').removeClass('active');
$(this).addClass('active');
return false;
}).filter(':first').click();
});
</script>
<div class="sidemenu">
<ul class="list">
<li class="active"><a href="#first">Login & Password</a></li>
<li><a href="#second">Contact Details</a></li>
<li><a href="#third">Company & Branch Details</a></li>
<li><a href="#forth">Address Details</a></li>
</ul>
</div>
<div class="pages">
<div id="first">
CONTENT 1
</div>
<div id="second">
CONTENT 2
</div>
<div id="third">
CONTENT 3
</div>
<div id="forth">
CONTENT 4
</div>
</div>
不知道我在这里错过了什么..也许是因为我刚刚醒来,还在喝第一杯咖啡..;)
I am trying to create a menu navigation sort of like tab's but with vertical buttons.. When I start the page, the first li class is removed and when I click any other link nothign happens other then my content div's being shown..
The first link should always be active on page start.
<script type="text/javascript">
$(document).ready(function() {
var tabContainers = $('div.pages > div');
$('div.sidemenu ul.list a').click(function () {
tabContainers.hide().filter(this.hash).show();
$('div.sidemenu ul.list li').removeClass('active');
$(this).addClass('active');
return false;
}).filter(':first').click();
});
</script>
<div class="sidemenu">
<ul class="list">
<li class="active"><a href="#first">Login & Password</a></li>
<li><a href="#second">Contact Details</a></li>
<li><a href="#third">Company & Branch Details</a></li>
<li><a href="#forth">Address Details</a></li>
</ul>
</div>
<div class="pages">
<div id="first">
CONTENT 1
</div>
<div id="second">
CONTENT 2
</div>
<div id="third">
CONTENT 3
</div>
<div id="forth">
CONTENT 4
</div>
</div>
Not sure what I am missing here.. Maybe its cuase I just woke up and still on my first cup of coffee.. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将该类添加到
元素中,但将其从其父
元素中删除。
看起来您打算让
拥有该类。所以你应该这样做:
或者如果你不介意我在其中混合一点 DOM API:
现在去补充一下! ;o)
You're adding the class to the
<a>
element, but removing it from its parent<li>
element.Looks like you intend for the
<li>
to have the class. So you'd do this instead:Or if you don't mind me mixing a little DOM API in:
Now go get a refill! ;o)
您将“活动”类添加到 A-Element
我猜您想将其添加到 LI-Element,因此您可以添加
或注册 LI-Element 上的 onclick
you add the "active" class to the A-Element
i guess you want to add it to the LI-Element, so either you add
or you register the onclick on the LI-Element