search

Home  >  Q&A  >  body text

Disabling Jquery click() event - Stack Overflow

$(document).ready(function(){
    $(".menua1").click(function(){
    //执行了点击事件的内容                    
            
    });
});
function toggleMenu(){
    var fs = window.top.document.getElementById("bframeset");
    if(条件true){
        $(".menua1_noclick").attr("class","menua1");
    }else{
        $(".menua1").attr("class","menua1_noclick");
    }
}
//HTML
<a class="menua1">A按钮</a>

When I click the B button and call toggleMenu(), the class value of the element is changed. I think the click event in ready() should be invalid. When I call toggleMenu(), I change the class value to the initial value. value, click() should be able to run normally;
But no matter how the class value of the a tag is changed, it will not affect my original click. If the class value is changed, I can still click to enter and execute the jqeury code, solve it.

My purpose is to disable the original click event of button A after clicking button B, and enable the original click event of button A when button B is clicked again.

Waiting online, please give advice.

伊谢尔伦伊谢尔伦2704 days ago1077

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:56:50

    When I clicked button B and called toggleMenu() to change the class value of the element, I felt that the click event in ready() should be invalid

    First of all, this understanding is wrong.

    Give me an example:

    There was a man named Xiao Ming. His parents called him Xiao Mingming, and his wife called him Da Mingming. One day, Xiao Ming’s hand was broken.
    His parents saw it and asked: Xiao Mingming, how did you break your hand? ?
    His wife said: Da Mingming accidentally cut it while cooking
    He turned around and asked Xiao Ming: Husband, do your hands still hurt?
    Dad/Mom said: Hey, they are so big Why do you still call me Da Mingming when you are dead? It doesn’t sound good
    My wife said: Okay, I won’t call you Da Mingming anymore

    Question: Is Xiao Ming’s hand broken?

    jQuery’s selector is used to select elements through different means,
    but the shanghai you cause to it will not be eliminated just because you change one of its attributes.

    So back to the topic, the click event is bound to the element and needs to be cleared

    1. 如果是js绑定:
    ele.onclick = function(){...}
    //要清除的话 , 用空方法覆盖
    ele.onclick = function(){}
    
    2. jQuery on方法绑定的点击事件
    //用off解除绑定
    $ele.on("click", function(){})
    $ele.off("click", function(){})
    
    3.或者在点击事件的回调中判断状态(全局变量/flag)
    var flag = true;
    $ele.on("click", function(){
      if(flag){
      ... 
      }
    });
    function toggle(){
      flag = !flag;
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:56:50

    The examples mentioned above are also interesting. To put it simply, the binding of events is only related to the element itself, and has little to do with its name in the future. It's like saying that your ID card represents you as a person, and then one day you change your name, does the ID card represent someone else? That’s the truth

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:56:50

    $(".menua1").off('click'), wouldn't it be enough to unbind the click event of the button

    reply
    0
  • Cancelreply