0
我遇到了jQuery範圍的問題,我想。我正在嘗試使用經典的jquery「greenify」教程插件,並且已經調整它以將所有錨鏈接的顏色更改爲由其各自的data-color屬性指定的顏色。但是,顏色始終是紅色的。有人能告訴我我做錯了什麼嗎?如何編寫一個與多個元素一起工作的jquery插件?
HTML:
<a data-color="red">Red</a>
<a data-color="yellow">Yellow</a>
<a data-color="black">Black</a>
JS:
(function ($) {
$.fn.colify = function() {
this.css("color", $(this).data('color'));
return this;
};
}(jQuery));
$('a').colify();
JS FIDDLE:http://jsfiddle.net/y4589gy2/
感謝您的幫助。這很有趣,我從來不知道你可以傳遞一個函數作爲CSS方法的第二個參數。 '(函數($){ $ .fn.myplugin =功能(選件){ \t \t:一些更多的研究,我發現,對於較大的插件後,我可以開始與線插件獲得所需的結果return this.each(function(){//其餘的插件代碼在這裏...}); }; }(jQuery)); – kohloth 2014-11-24 15:29:25