在取消绑定元素后,我需要有关绑定的帮助。基本上,我有三个锚元素,我想实现这个目标:
单击其中一个时,无法单击其他两个。然后,当用户单击关闭按钮时,我想绑定所有三个链接的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');
});
答案 0 :(得分:8)
请勿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;
}
答案 1 :(得分:5)
似乎您不需要取消绑定/重新绑定以获得所需的结果。只需使用一面旗帜。即。
//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,而不是简写点击功能......