我希望有人可以帮我弄清楚如何创建两个执行相同操作的按钮。目前,我有一个底部抽屉,可打开一个可见的可点击标签。我想创建一个执行相同操作的附加按钮,但我不太确定如何编写JQuery函数。
我目前最底层抽屉的JQuery如下。如果它提供了更多的背景,我也会联系到一个小提琴。 My Fiddle
var h_bt = 0; //variable to keep track of the div#bottom condition.
$('a#handler_bottom').on('click', function(event){
event.preventDefault();
if(h_bt == 0){
//$('div#bottom').animate({top:'600px'}, 300);
//$('div#top').css({'background-color': 'red'});
$(this).parent().animate({top:'480px'}, 300);
$(this).css({'background-color': '#838da6'});
h_bt = 1;
}else{
$(this).parent().animate({top:'600px'}, 300);
$(this).css({'background-color': '#838da6'});
h_bt = 0;
}
});
我很感激帮助!
答案 0 :(得分:0)
创建一个函数并将其附加到两个事件: -
function repeatFunction(event){
event.preventDefault();
if(h_bt == 0){
//$('div#bottom').animate({top:'600px'}, 300);
//$('div#top').css({'background-color': 'red'});
$(this).parent().animate({top:'480px'}, 300);
$(this).css({'background-color': '#838da6'});
h_bt = 1;
}else{
$(this).parent().animate({top:'600px'}, 300);
$(this).css({'background-color': '#838da6'});
h_bt = 0;
}
}
将它应用于以下事件: -
$(函数(){ $(部件1)。点击(函数(事件){ repeatFunction(事件); });
$(element2的)。单击(功能(事件){ repeatFunction(事件); }); });