jquery - 禁用/取消绑定(bind)点击事件元素,在不活动时重新绑定(bind)点击

标签 jquery html css click unbind

我有一个左侧导航,可在右侧显示/隐藏内容。目前,当您单击一个链接时,它会在右侧淡入相应的内容,并向该链接添加一个事件类。我的问题是,如果您再次单击事件链接,右侧的内容会再次淡入。我想在链接处于事件状态时取消绑定(bind)该点击,如果您点击另一个导航链接(随后从上一个链接中删除该类并将其添加到当前链接),则将点击事件重新绑定(bind)到所有非事件链接。

这是我当前的代码:

    $('.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');
});

非常感谢您的帮助!

最佳答案

如果您检查 $( this ) 是否已激活,如果没有,则继续执行代码会怎么样。

所以,在函数的开始,去:

if( ! $( this ).hasClass( 'active' ) )
{
  // do stuff
}

关于jquery - 禁用/取消绑定(bind)点击事件元素,在不活动时重新绑定(bind)点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2672382/

相关文章:

javascript - 在给定元素之后查找实例

javascript - 如何从 nodeList 获取元素?如何使用byTagname/ByClassName?

html - 是否可以仅使用 CSS 隐藏悬停图像并显示另一幅图像?

html - Chrome 在少数情况下不遵循@media 打印规则

html - 使分页保持在页面的中心

javascript - 使用 Javascript 根据组中的值选中或取消选中复选框

javascript - 使用下拉列表填充 HTML 页面

CSS - 如何避免元素内容分两行显示

javascript - 单击 "<button>"时提交表单

css - 除了左/右/上/下之外,还有什么可以控制绝对定位元素的放置?