【问题标题】:How to bind after unbind in jQuery?在jQuery中取消绑定后如何绑定?
【发布时间】:2012-01-11 19:40:42
【问题描述】:

我在取消绑定元素后需要帮助我重新绑定。基本上,我有三个锚元素,我想实现这个:

当其中一个被点击时,其他两个不能被点击。然后当用户点击关闭按钮时,我想绑定所有三个链接的点击事件。

解绑没问题,但是绑定回来就不行了。我没有发布任何代码,因为我认为提供的解决方案一般都可以工作,而不仅仅是我的情况。

是否有任何其他方法可以实现我想要的,但不是使用绑定/取消绑定或开/关?

编辑:

我是这样做的。动画 div 称为 miniContainer。这是一个锚点的示例,但三个锚点都是一样的。

       //First Link         
krug1Link.click(function(element){
   if(miniContainer.hasClass('animirano')){
       return false;
   };
   element.preventDefault();
   miniContainer.stop().animate({ height:360 },{duration:500,easing: 'easeOutBack'});
   miniTekst1.animate({opacity:'1'},1200);
   polaroidMali.animate({opacity:'1'},1200);
   miniContainer.addClass('animirano');
});

//Close Mini Container
close.click(function(element){
    miniTekst1.animate({opacity:'0'},1200);
    miniTekst2.animate({opacity:'0'},1200);
    polaroidMali.animate({opacity:'0'},1200);
    polaroidMali2.animate({opacity:'0'},1200);
    miniContainer.animate({marginTop: 10, height:1},{duration:600,easing: 'easeInBack'});
    miniContainer.removeClass('animirano');
});

【问题讨论】:

  • @PastorBones:这个问题是关于禁用链接的默认行为,而不是关于禁用处理程序本身。

标签: javascript binding jquery


【解决方案1】:

不要bind 和unbind 这样做。

选择一个共同的祖先,并使用 jQuery 的基于选择器的事件委托。

var clickables = $('.clickable');

$('#some_ancestor').delegate( '.clickable', 'click', function() {
    // to disable the others
    clickables.not( this ).removeClass('clickable');
});

然后要重新启用它们,只需重新添加类即可。

clickables.addClass( 'clickable' );

如果您使用的是 jQuery 1.7 或更高版本,请改用 .on(),因为它内置了事件委托。

$('#some_ancestor').on( 'click', '.clickable', function() {
    // to disable the others
    clickables.not( this ).removeClass('clickable');
});

这是来自以下 cmets 的解决方案之一。

演示:http://jsfiddle.net/c9Ydy/3/

<ul id="container"> 
    <li id="link1" class="box clickable">
        <a href="#">LINK ONE</a>
    </li>
    <li id="link2" class="box clickable">
        <a href="#">LINK TWO</a>
    </li>
    <li id="link3" class="box clickable">
        <a href="#">LINK THREE</a>
    </li>
</ul>

<div id="content_display">
    <button>CLOSE</button>
    <p id="link1_content">THIS IS THE CONTENT FOR THE FIRST LINK.</p>
    <p id="link2_content">THIS ONE IS FOR THE SECOND LINK</p>
    <p id="link3_content">AND THIS IS THE THIRD LINK</p>
</div>

js

var clickables = $('.clickable'),
    display = $('#content_display'),

    $content, // remember the one that was clicked
    display_animation_enabled = false;

$('#container').delegate( '.clickable', 'click', function() {

    display_animation_enabled = true;
    clickables.removeClass('clickable');

    if( $content ) { $content.hide(); }

    $content = $('#' + this.id + '_content').show();
    cycle();
});
display.find('p').hide();
display.find('button').click(function() {
    display_animation_enabled = false;
    clickables.addClass( 'clickable' );
    if( $content ) { $content.hide(); }
});
function cycle() {
    if( display_animation_enabled ) {
        display.animate({width:300,height:300})
               .animate({width:200,height:200}, cycle);
    }
}

css

div.box {
    width: 50px;
    height: 50px;
    background: yellow;
    margin: 10px;
}

div.clickable {
    background: orange;
}

#content_display {
    width: 200px;
    height: 200px;
    background: yellow;
}
#content_display > p {
    margin: 10px;
    color: blue;
}

【讨论】:

  • 感谢 RightSaidFred,但如果我要掌握你写的内容,我需要更多的手。我知道一些 jQuery,但设计是我的面包和黄油。
  • 请问您对我的解决方法有何看法,当单击任何链接时,新的 div 会被动画化,因此我在每次单击事件时向该 div 添加了一个类。但是第一行单击事件检查 div 是否应用了一个类,如果有,则返回 false。我通过单击关闭按钮来删除该类。我的解决方案是否好,或者我应该使用委托、绑定、开/关使事情复杂化?
  • @suludi:你当然可以这样做。我个人更喜欢事件委托。发生的情况是您将处理程序绑定到某个公共祖先,因此每个可点击元素都在该公共容器中。当用户点击页面时,点击事件“冒泡”。这意味着单击从嵌套最深的元素开始,然后向上运行,尝试触发每个元素上的处理程序,直到到达window,并且没有进一步的余地。因此,它会在途中遇到那个共同的祖先并触发它的处理程序。
  • ...here's an update 从数组中获取内容。它获取被点击链接的.index(),并使用该索引从数组中获取内容。
  • @suludi: Here's another update 像你描述的那样显示和隐藏。我给每个 li 和 id 以及每个内容部分提供了相同的 id,但添加了 _content。这使得从单击的li 中选择相关内容部分变得很容易。它还将当前部分存储在content 变量中,以便在单击关闭按钮时轻松隐藏它。
【解决方案2】:

您似乎不需要解除绑定/重新绑定即可获得所需的结果。只需使用一个标志。即

//This is a variable that you will use to check whether any of the anchors have been
//clicked. If this flag is set to true, then the click handler below will not 
//perform any action when the other anchors are clicked. When this flag is set to false
//the click handler will take the desired action. From your description it sounds like
//the action might be opening a window of some sort...  
var buttonClicked = false;

//Bind a click handler to each anchor
$('#anchor-1').click(function(){

     //check to see if one of the anchors has already been clicked. If none have been
     //clicked take execute the code inside the if statement.
     if(!buttonClicked){

          //set the flag to true, indicating that one of the anchors has been clicked.
          buttonClicked = true;

          //Here you would open your window or popup...
          //i.e. popup.open();
     }
});

$('#anchor-2').click(function(){
     if(!buttonClicked){
           //logic for second anchor..
     }
});

//Bind a click handler to the close button you mentioned
$('.close-button').click(function(){

     //Close the window or popup here...
     //i.e. popup.close();

     //set the flag to false, which will allow all of the anchors to be clicked.
     buttonClicked = false;
});

如果您的关闭按钮是动态生成的,您将需要使用 live、delegate 或 on,而不是速记单击功能...

【讨论】:

  • 请您更详细地解释您的答案,设计是我的主要优势,而不是 jQuery 和编程。这是否意味着单击其中一个链接时不会发生任何事情,因为这是我的行为想要。
  • @suludi 当然。在我的回答中查看我的其他 cmets。
  • 感谢 Mark 的努力。打开的不是窗口,而是 div 容器的动画。我自己设法做到了,当单击其中一个链接时,我正在添加一个类div。我用关闭按钮删除它。所以每个链接首先检查 div 是否应用了一个类,如果它返回 false,那么什么也不会发生。如果它没有运行我的代码。我现在将尝试你的解决方案,如果很好,我会将您的答案标记为正确。谢谢!
  • 嗨,马克,问题是我的所有三个链接在单击时都会执行不同的操作(主要是动画、效果等),我只是无法在您的解决方案中实现它。无论如何,谢谢,我现在很好,因为我设法通过向动画 div 添加类来解决它。
  • @suludi 该过程将完全相同。您只需检查每个单击处理程序中的标志。查看更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多