jQuery基本插件不起作用

jQuery basic plugin not working?

本文关键字:不起作用 插件 jQuery      更新时间:2023-09-26

我从jQuery的网站复制了基本插件。现在,当我将此代码放入文件中并将其加载到我的应用程序中时,该插件不起作用。我无法通过$.访问该插件,但可以看到通过$.fn.访问它(这对我来说似乎很奇怪)。这是我加载脚本的方式(我什么也没看到)。

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery-ui.js"></script>
<script type="text/javascript" src="plugins.js"></script>
<script type="text/javascript" src="index.js"></script>

这是plugins.js里面的东西

$.fn.greenify = function() {
    this.css( "color", "green" );
    return this;
}

我什至尝试了以下方法:

(function ( $ ) {
$.fn.greenify = function( options ) {
    // This is the easiest way to have default options.
    var settings = $.extend({
        // These are the defaults.
        color: "#556b2f",
        backgroundColor: "white"
    }, options );
    // Greenify the collection based on the settings variable.
    return this.css({
        color: settings.color,
        backgroundColor: settings.backgroundColor
    });
}; }( jQuery ));

但这似乎也行不通。控制台也不会显示任何错误。

问题是你如何调用它应该是的插件

$('#almightyGreen').click(function () {
    $('.peasants').greenify();
});

您的代码$.greenify('.peasants');正在尝试调用由$.greenify引用的函数,该函数在您共享的代码中不存在。