我的代码正在运行,但要求我点击两次以激活我的链接(一次用于点击事件,一次用于切换事件.)我该怎么做才能使我只需点击一次以便切换将自动发生?
//Show or Hide Comments
$('#showHideComments').live('click', function() {
$('#showHideComments').toggle(function() {
$(".commentsSwitch").animate({"marginLeft": "+=53px"}, 500);
$("#comments").fadeIn(300);
$("#addComment").fadeIn(300);
},function(){
$(".commentsSwitch").animate({"marginLeft": "-=53px"}, 500);
$("#comments").fadeOut(300);
$("#addComment").fadeOut(300);
});
});
Run Code Online (Sandbox Code Playgroud)
谢谢!
Dou*_*ner 42
不能使用live和toggle在一起.你可以做的,只是让你自己"切换"各种各样:
$('#showHideComments').live('click', function() {
if($("#addComment").is(':visible')){
$(".commentsSwitch").animate({"marginLeft": "-=53px"}, 500);
$("#comments, #addComment").fadeOut(300);
} else {
$(".commentsSwitch").animate({"marginLeft": "+=53px"}, 500);
$("#comments, #addComment").fadeIn(300);
};
});
Run Code Online (Sandbox Code Playgroud)
live绑定到click,然而,当toggle被调用时,它也必然(常)上点击.你应该决定'live'是否真的是你需要的.例如,如果#showHideComments在使用页面期间通过AJAX替换了对象,那么您需要实时和我的解决方案.但是,如果它没有换出并保持不变,只需使用原始live功能的内部(只需切换代码),你就会变得金色.
| 归档时间: |
|
| 查看次数: |
21624 次 |
| 最近记录: |