爲了讓我的問題更加具體,我閱讀了關於.each()的jQuery文檔,但我有點困惑。我有這樣的代碼:在jQuery中,this.each()是如何工作的?
$.fn.imgAreaSelect = function (options) {
options = options || {};
this.each(function() {
if ($(this).data('imgAreaSelect')) {
if (options.remove) {
$(this).data('imgAreaSelect').remove();
$(this).removeData('imgAreaSelect');
}
else
$(this).data('imgAreaSelect').setOptions(options);
}
else if (!options.remove) {
if (options.enable === undefined && options.disable === undefined)
options.enable = true;
$(this).data('imgAreaSelect', new $.imgAreaSelect(this, options));
}
});
if (options.instance)
return $(this).data('imgAreaSelect');
return this;
};
現在我不明白這是爲什麼每個函數都沒有索引或元素?這段代碼來自我試圖閱讀的jQuery插件。我也不太瞭解$ .fn。在頂部,我知道它代表原型,但是到底發生了什麼?
但看看第三行的代碼:)'this.each(函數({'這一點。具體來說,是什麼'this.each('做?我懷疑它是一樣的,因爲我們沒有看到'this'被封裝爲一個jQuery對象。 – AjaxLeung 2015-08-25 06:58:03