这是编写我的 javascript 时经常出现的问题:我有事件:
$(document).on('click','.mi_piace_brick',function(){
$this = $(this).children(),
$that = $(this),
i = s.get_current_step_id(),
j = $that.attr('data-class'),
counter = parseInt($('.like_counter_'+i+'_'+j).first().find('p').text());
if($this.hasClass('liked')){
$this.removeClass('liked');
$('.like_counter_'+i+'_'+j).find('p').text(--counter);
}
else{
$this.addClass('liked');
$('.like_counter_'+i+'_'+j).find('p').text(++counter);
}
})
和
$(document).on('click','.pref_brick',function(){
$this = $(this).children(),
$that = $(this),
i = s.get_current_step_id(),
j = $that.attr('data-class'),
counter = parseInt($('.fav_counter_'+i+'_'+j).first().find('p').text());
if($this.hasClass('preferito')){
$this.removeClass('preferito');
$('.fav_counter_'+i+'_'+j).find('p').text(--counter);
}
else{
$this.addClass('preferito');
$('.fav_counter_'+i+'_'+j).find('p').text(++counter);
}
})
如您所见,这两个功能基本上非常相似。我经常遇到这种情况。在您看来,有一种方法可以只使用一个绑定来解决问题吗?