2011-04-09 107 views
0

我的腳本:jQuery的委託不工作

(function($){ 
$.fn.megaswitcher = function(settings) { 
    return this.each(function() {   
     var $i = $(this), 
      current, 
      childs = $i.find('.selection li').length; // returns desired number 

     $i.find('.selection li').delegate('.active', 'dblclick', function(e){ 
      e.preventDefault(); 
      current = $i.find('.selection li').index(this); 
      alert('triggered @ ' + current); // doesn't even execute 
      var _delay = $(this).attr('name') > 0 ? (parseInt($(this).attr('name')) * 1000) : 5000; 
      $(this).delay(_delay).show(0, function(){ 
       if((current + 1) < childs){ // if not last 
        $(this).removeClass('active').next().addClass('active').show(0, function(){ 
         $i.find('.image img').addClass('tempp'); 
         $(this).find('img').clone().hide().addClass('temp').appendTo($i.find('.image')).fadeIn(400, function(){ 
          $i.find('.image img.tempp').remove(); 
         }); 
        }).trigger('dblclick'); 
       }else{ 
        $(this).removeClass('active'); 
        $i.find('.selection li:first').addClass('active').show(0, function(){ 
         $i.find('.image img').addClass('tempp'); 
         $(this).find('img').clone().hide().addClass('temp').appendTo($i.find('.image')).fadeIn(400, function(){ 
          $i.find('.image img.tempp').remove(); 
         }); 
        }).trigger('dblclick'); 
       } 
      }); 
     }); 

     $i.find('.selection li.active').trigger('dblclick'); 
    });  
}; 
})(jQuery); 

得承認,這,這是一個巨大的混亂在那裏,但我不知道爲什麼該委託是不工作...

任何想法?

P.S.我有一個基於與dblclick相同的技術的不同插件,除了它沒有使用find();進行項目選擇,我有一種感覺,這是導致問題的原因,但我不知道取代它。

在此先感謝!

+0

又是什麼的HTML是什麼樣子? – typeof 2011-04-09 19:26:06

回答

3

您必須在要附加事件的項目的父項上調用委託。 相反的:

$i.find('.selection li').delegate('.active' ... 

你應該做的

$i.find('.selection').delegate('li.active' 
+0

謝謝一堆,它做到了! – jolt 2011-04-10 12:40:29