2013-02-25 56 views
0

我有我的网站上的两个按钮充当一个切换到允许“包括”“排除”模式发生。包含按钮默认突出显示。如何打开和关闭了不少的jQuery单击激活的功能,然后让他们重新打开

这是为两个按钮的HTML:

<div class="smallmenu women abs on" id="include"><div class="text">include</div></div> 
<div class="smallmenu men abs hov" id="exclude"><div class="text">exclude</div></div> 

包括默认为激活(因此在“接通”类)。当一个按钮处于“开启”状态时,我不希望人们能够将其悬停并看到效果(这就是为什么include没有'hov'类并排除的原因),我不希望点击它做任何事情。要切换模式,我希望人们不得不点击另一个按钮。

我能够让我想发生与jQuery的当有人点击了“排除”按钮的影响,我能作出这样的按钮停止活跃,一旦被点击(与$("#exclude").unbind();),但当有人然后点击'包括'按钮我不知道如何使''排除'按钮变得活跃了。

我也不知道如何阻止'include'按钮在页面首次加载时处于活动状态。但我还没有真正玩过这部分。

下面是一些代码:

$("#exclude").click(function() { 
    $(this).toggleClass("on"); 
    $(".filtercategory").toggleClass("inc"); 
    $("#include").toggleClass("on"); 
    $("#include").toggleClass("hov"); 
    $(this).toggleClass("hov"); 
    $("#alternatefilterinfo").toggleClass("hide"); 
    $("#defaultfilterinfo").toggleClass("hide"); 
    $("#exclude").unbind(); 
     }); 


$("#include").click(function() { 
    $(this).toggleClass("on"); 
    $(".filtercategory").toggleClass("inc"); 
    $("#exclude").toggleClass("on"); 
    $("#exclude").toggleClass("hov"); 
    $(this).toggleClass("hov"); 
    $("#exclude").bind(); //this line fails to do anything! 
}) 

回答

1

.bind()函数不具有先前去除的处理程序(一个或多个)的“记忆”。正如在the .bind() documentation中所解释的那样,您必须将其传递给一个函数,方法与将其传递给.click()的方法相同。

而不是试图解除绑定,然后重新绑定,这个怎么样:

$("#exclude").click(function() { 
    if ($(this).hasClass("on")) { 
     return; 
    } 
    // your other code here 
}); 

...并为其他类似的按钮。也就是说,当点击控件时,检查它是否已经“开启”,如果是,则不做任何事情(即立即返回)。

1

如果您想让global开关打开/关闭事件,则不会有一个。但是你可以通过在触发事件的函数中检查一个全局变量来'嘲笑'。

例如:

var eventsSwitchedOn = true; //global switch 

$("#mybutton").click(function() { 
    if(eventsSwitchedOn) { alert("I am allowed to fire a click event!"); } 
}); 

//now you can test it like this: 

eventsSwitchedOn = false; 

$("#mybutton").click(); //will do nothing 

eventsSwitchedOn = true; 

$("#mybutton").click(); //will alert the message