2012-04-17 82 views
1

這裏是我的插件的代碼..

$.fn.slide = function(settings) { 

return $(this).each(function() { 
    setInterval(function() { $.slider(opt.direction , opt.slideSpeed,this) } 
} 

jQuery.slider = function(direction,slideSpeed,elm) { 
     console.log(elm) - > shows DOMWindow[] window as object 
    } 

} 


script.js 
$('#container').slide({ 
    slideAnimationTimeInterval : 6000, 
    slideSpeed : 700, 

}); 

的console.log(榆樹) - >顯示DOMWindow []窗口對象,但我需要的#container反對我怎樣才能得到它

回答

4

當嵌套功能,你需要在另外一個變量保存this是這樣的:

return $(this).each(function() { 
    var self = this; 
    setInterval(function() { $.slider(opt.direction , opt.slideSpeed, self); } 
} 

this是函數的上下文,默認情況下它是全局對象window。調用函數時,jQuery將它設置爲更有用的東西(例如.each()中的元素)。但是,如果調用間隔函數,則this再次解除綁定(=>this === window)。通過將其保存在一個自定義變量中,它會保存在函數的關閉中。

+0

感謝一噸thiefmaster :) – user1184100 2012-04-17 10:08:02

0

只要將它轉換成一個jQuery對象:

var $elm = $(elm); 
相關問題