javascript jQuery 切换 .click() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7676643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery toggle .click() function
提问by A_funs
I am unsure if this is a "callback' that I need or not, but basically I am trying to emulate the reversal of the function like on the .hover() effect with the .click() effect. Is this possible. So basically, if the user clicks again on the link, the reverse of what just happened, ie a div closing as opposed to opening, will happen. Is it possible to do this with .click()?
我不确定这是否是我需要的“回调”,但基本上我试图用 .click() 效果模拟 .hover() 效果上的函数反转。这可能。所以基本上, 如果用户再次点击链接,就会发生与刚刚发生的相反的事情,即 div 关闭而不是打开。是否可以使用 .click() 来做到这一点?
$(document).ready(function(){
$('.content_accrd').css({"height":"0px"});
$('.paneltop').click( function() {
$(this).css({"background-position":"0px -21px"})
.siblings().animate({"height":"100px"}, 200)
}, function() {
$(this).css({"background-position":"0px 0px"})
.siblings().animate({"height":"0px"}, 200);
})
})
回答by Matt Ball
回答by genesis
try toggle
尝试切换
$(".paneltop").toggle(function(){
//first click happened
},function(){
//second click happened
});
回答by njr101
No, the click action will always do the same thing. If you want it to have alternating effects you will have to check the current state of the element and switch it to the new state accordingly.
不,单击操作将始终执行相同的操作。如果您希望它具有交替效果,则必须检查元素的当前状态并相应地将其切换到新状态。
This looks something like:
这看起来像:
$('#foo').click(function () {
if ( $('#foo').is(':visible') ) {
$('#foo').hide();
} else {
$('#foo').show();
}
});
Depending on the effect you are trying to achieve this could be any change you want.
根据您尝试实现的效果,这可能是您想要的任何更改。