2012-03-11 48 views
0

我想通过live()方法将函数与函数绑定。 该功能仅在第一次使用时表现良好。 我认为我必须从任何事件中解开这个元素并反弹相同的功能,但我不知道该怎么做!回弹相同功能

下面是代码:

var temp = function() { 
    var htmlEx = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

    $('#template_loading').fadeIn(); 
    $('#template_loading').queue(function() { 
     $('#tp_prev').html(htmlEx); 
     $('#template_container').removeClass("cur_temp"); 
     $('#template_container').addClass("cur_prev"); 
     $('#template_container').animate({"margin-left" : "0"}, 400, 'easeOutExpo');  
     $('#template_container').queue(function() { 
      $('#template_loading').fadeOut();    
      $('#tp_cur').empty();    
      $('#template_container').removeClass("cur_prev"); 
      $('#template_container').addClass("cur_temp"); 
      $('#tp_prev').empty();    
      $('#tp_cur').html(htmlEx); 
      $('#tp_cur').queue(function() { 
       $('#prev.pers_arrow').die(); 
       $('#prev.pers_arrow').live("click", temp); 
       $(this).dequeue(); 
      }); 
      $(this).dequeue(); 
     }); 
     $(this).dequeue(); 
    }); 
}; 

$('#prev.pers_arrow').live("click", temp); 
+0

**该代码没有按”没有道理!**你删除了事件,然后再次添加它,并且函数的回调函数将自身添加为回调函数。 ** ha?!?!** – gdoron 2012-03-11 11:45:49

+1

不,您通常不必删除并添加事件侦听器来多次运行它们。你可能对函数本身有问题。如果您详细说明实际问题,您想要实现什么,并解释代码应该做什么以及它做了什么,我们可以帮助您更好地实现。包括http://jsfiddle.net/演示会更好。 – 2012-03-11 11:47:20

回答

1

第一:永远,永远,永远做这样的。

你必须缓存你的数据,不要一直跳到最高!

第二: 在我看来,现场已被弃用,你可以在和使用- 所以关闭

尝试:

var prev=$("#prev"); 
var pers_arrow=".pers_arrow"; 
    var temp = function() { 
    var htmlEx = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
    var template_loading=$('#template_loading'); 

    template_loading 
     .fadeIn() 
     .queue(function() { 
      $('#tp_prev').html(htmlEx); 
      var template_container=$('#template_container'); 
      template_container 
       .removeClass("cur_temp") 
       .addClass("cur_prev") 
       .animate({"margin-left" : "0"}, 400, 'easeOutExpo') 
       .queue(function() { 
        template_loading.fadeOut();    

        template_container.removeClass("cur_prev").addClass("cur_temp"); 
        $('#tp_prev').empty();  
        //you can don't use it - because .html() method already will clean container   
        //$('#tp_cur').empty();  
        $('#tp_cur').html(htmlEx).queue(function() { 
         prev.off("click",pers_arrow,temp).on("click",pers_arrow,temp); 
         $(this).dequeue(); 
        }); 
        $(this).dequeue(); 
       }); 
      $(this).dequeue(); 
     }); 
}; 
prev.off("click",pers_arrow,temp).on("click",pers_arrow,temp) 
+0

谢谢你的回答! – kapantzak 2012-03-15 09:22:47