我正在爲jQuery創建一個插件。我不會嘗試在這裏解釋插件,所以讓我們簡單地說,當你點擊目標元素時,我的插件會打開警報。這是我的插件的簡單版本。
(function($) {
// Options
var defaults = {
message: 'Default message'
};
var options = $.extend(defaults, options);
$.fn.jAlert = function(options) {
return this.each(function(){
var $this = $(this);
$this.click(function(){
alert(options.message);
});
});
};
})(jQuery);
我可以得到那麼多。它效果很好。但是,如果我把我的插件是這樣的:
$('h1.simon').plugin({ message: 'Hello ' + $(this).attr('class') });
的消息「你好未定義」,我寧願它是「你好西蒙」(類的H1標籤的)返回。
我確定這是最簡單的事情,但我甚至不知道我應該用Google去尋找解決方案。
任何幫助將不勝感激!
乾杯,
威爾
更新:
約打了一下後,這似乎工作...我不知道爲什麼!我不認爲我真的瞭解範圍。我想我會去做一些閱讀。
$('h1.simon').click(function(){
$(this).jAlert({
icon: 'Hello ' + $(this).attr('class')
});
});
$(這)是指你指的是任何封閉範圍,而不是對象。 – 2010-07-14 00:04:11