0

私のサイトには、「含める」モードまたは「除外する」モードを有効にするトグルとして機能する 2 つのボタンがあります。含めるボタンはデフォルトで強調表示されています。

これは、2 つのボタンの 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>

インクルードはデフォルトで有効になっています (したがって、「on」クラス)。ボタンが「オン」の場合、人々がそれにカーソルを合わせて効果を確認できるようにしたくありません (これが、インクルードに「hov」クラスがなく、除外する理由です)。何でもする。モードを切り替えるには、他のボタンをクリックする必要があります。

誰かが「除外」ボタンをクリックすると、 jQueryで必要な効果を発生させることができます。また、クリックするとそのボタンがアクティブになるのを止めることができます( )が、誰かが「含める」$("#exclude").unbind();をクリックすると'ボタン'除外'ボタンを再度アクティブにする方法がわかりません。

また、ページが最初に読み込まれたときに「含める」ボタンがアクティブにならないようにする方法もわかりません。しかし、私はまだこの部分で実際に遊んでいません。

ここにいくつかのコードがあります:

$("#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!
})
4

2 に答える 2

1

関数には、.bind()以前に削除されたハンドラーの「メモリ」がありません。ドキュメントで説明され.bind()いるように、 に渡すのと同じ方法で関数を渡す必要があります.click()

バインドを解除してから再バインドしようとするのではなく、次のようにします。

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

...他のボタンについても同様です。つまり、コントロールがクリックされると、それが既に「オン」になっているかどうかを確認し、そうであれば何もしません (つまり、すぐに戻ります)。

于 2013-02-25T08:09:28.383 に答える
1

If you want a global switch to turn events on/off, there isn't one. But you can 'mock' one by having a global variable that is checked within the function of the triggered event.

For example:

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
于 2013-02-25T09:03:29.053 に答える