[英]How to bind after unbind in jQuery?
在取消綁定元素后,我需要有關綁定的幫助。 基本上,我有三個錨元素,我想實現這個目標:
單擊其中一個時,無法單擊其他兩個。 然后,當用戶單擊關閉按鈕時,我想綁定所有三個鏈接的click事件。
取消綁定不是問題,但綁定后退不起作用。 我沒有發布任何代碼,因為我認為提供的解決方案一般都有效,而不僅僅是在我的情況下。
有沒有其他方法可以實現我想要的,但不是使用bind / unbind或on / off?
編輯:
這就是我如何做到的。動畫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');
});
不要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');
});
以下是以下評論中的解決方案之一。
演示: 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;
}
似乎您不需要解除綁定/重新綁定以獲得所需的結果。 只需使用一面旗幟。 即
//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,而不是速記點擊功能......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.