2011-02-07 52 views
1

我要調用此函數(使元素閃爍),聲明爲一個單獨的文件:

(function($) { 
$.fn.cyclicFade = function(options) 
{ 
    if (typeof(options) == 'string') { 
     if (options=='stop') { 
      $(this).stop(true); 
      return this.each(function() { 
       $(this).data('cyclic-fade').enabled = false; 
      }); 
     } 
     else return null; 
    } 
    else { 
     var opts = $.extend({}, $.fn.cyclicFade.defaults, options); 
     return this.each(function() { 
      $(this).data('cyclic-fade', {enabled : true}); 
      $.fn.cyclicFade.doCycle(this,1,opts.repeat,opts.params,0); 
     }); 
    } 
}; 
} 

(它不是完整的,空間問題)

當我點擊一個名爲.swatch的div。我如何invoque該功能?

$(document).ready(function() { 
    $(".swatch").click(function() { 
     // .swatch starts to blink 
    }); 
}); 

在此先感謝。

回答

3
$(document).ready(function() { 
    $(".swatch").click(function() { 
    $(this).cyclicFade(); 
    }); 
}); 

在功能方面this是選擇匹配的元素集合傳遞給$(...)。

+1

還要提到的是`$ .fn.pluginName`使得pluginName jQuery對象的擴展 – mVChr 2011-02-07 18:41:56

1

檢查這一項:

$(this).cyclicFade(); 
相關問題