美好的一天,簡單的jQuery插件不工作
我正在寫我的第一個jQuery插件。我從學習jQuery網站抓取了代碼。下面是使用JavaScript的HTML:
<div>Hello World</div>
<br>
<br>
<div>Hola Mundo</div>
<a href="http://www.nbcnews.com">My Link</a>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/jquery.greenify.js"></script>
<script>
$(function() {
$('div').greenify({
text: "Hola",
color: "#FF0000"
});
});
這裏是jquery.greenify.js:
(function($) {
$.fn.greenify = function(options) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
text: "Somewhere in time",
color: "#556b2f",
backgroundColor: "white"
}, options);
return this.each(function() {
$(this).text(settings.text);
if (settings.color) {
$(this).css('color', settings.color);
}
if (setting.backgroundColor) {
$(this).css('background-color', settings.backgroundColor);
}
});
};
}(jQuery));
無論出於何種原因,我沒有看到變化發生作用。我已經檢查了控制檯,沒有錯誤。我試圖設置斷點,並在jquery.greenify.js中返回this.each以查看代碼是否正在執行,而不是。我從Github下載了一個類似的例子https://github.com/taupecat/jquery-plugin那確實工作。之後,我試圖對代碼進行建模,但仍然沒有任何運氣。
我希望另一組眼睛會捕捉我明顯缺少的東西。
我已經檢查了這個網站上的其他一些問題,但這些答案與我的問題無關。
TIA,
COSON
錯字:'setting.backgroundColor'應該是'settings.backgroundColor' 。我的JS控制檯出現錯誤。 – Barmar
修復後,它適用於我:http://jsfiddle.net/barmar/5rcrv4wt/1/ – Barmar
@Barmar,謝謝你的收穫。 – coson