禁用/取消绑定活动项目上的点击,在不活动时重新绑定点击
我有一个左侧导航,可以显示/隐藏右侧的内容。目前,当您单击链接时,它会淡入右侧相应的内容,并向该链接添加一个活动类。我的问题是,如果您再次单击活动链接,右侧的内容会再次淡入。我想在链接处于活动状态时取消绑定该单击,并且如果您单击另一个导航链接(随后从前一个链接中删除该类并将其添加到当前链接中),则将单击事件重新绑定到所有非活动链接。
这是我当前的代码:
$('.mixesSidebar ul li').click( function() {
//Get the attribute id
var liId = $(this).attr('id');
//removeClass active from all li's, addClass active to this
$('.mixesSidebar ul li').removeClass('active');
$(this).addClass('active');
//Hide all content on the right
$('.mixesContent ul').hide();
//Find the content with the same class as the clicked li's ID, and fadeIn
$('.mixesContent').find('.' + liId).fadeIn('slow');
});
非常感谢您的帮助!
I have a left-positioned navigation that shows/hides content on the right. Currently, when you click a link, it fades in the corresponding content on the right and adds an active class to that link. My problem is that if you click the active link again, the content on the right keeps on fading in again. I would like to unbind that click while the link is active, and if you click on another navigation link (subsequently removing the class from the previous link and adding it to the current one) rebind the click event to all inactive links.
Here is my current code:
$('.mixesSidebar ul li').click( function() {
//Get the attribute id
var liId = $(this).attr('id');
//removeClass active from all li's, addClass active to this
$('.mixesSidebar ul li').removeClass('active');
$(this).addClass('active');
//Hide all content on the right
$('.mixesContent ul').hide();
//Find the content with the same class as the clicked li's ID, and fadeIn
$('.mixesContent').find('.' + liId).fadeIn('slow');
});
Thanks so much for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您检查
$( this )
是否已处于活动状态,如果没有,则继续执行代码会怎样。因此,在函数的开头,执行:
What if you checked if
$( this )
was already active, and if not, then proceed with the code.So, at the start of the function, go:
围绕逻辑:
比解除绑定和重新绑定稳定得多。
Surround the logic with:
A lot more stable than unbinding and rebinding.
如果我理解正确的话,我只会向淡入的 div 添加一个“活动”或某个类,然后检查它是否已经具有该类,如果有,则不要淡出它。
编辑因为我的描述清晰如泥:
If I understand this correctly, I would just add an "active" or something class to the div that was faded in and then do a check to see if it has that class already or not and if it does, don't fade it.
Edit Because my description is clear as mud: