没有定义从javascript获取错误$加载jquery库

loading jquery library from javascript getting error $ is not defined

本文关键字:加载 jquery 取错误 获取 定义 javascript      更新时间:2023-09-26

我已经加载了我的jquery库在我的插件从javascript和得到一个错误$没有定义如果我从另一个javascript页面调用我的插件匿名函数以下是我的工作

(function () {
    // Localize jQuery variable
    var jQuery;
    /******** Load jQuery if not present *********/
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.11.3') {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src",
            "https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js");
        if (script_tag.readyState) {
            script_tag.onreadystatechange = function () { // For old versions of IE
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    scriptLoadHandler();
                }
            };
        } else {
            script_tag.onload = scriptLoadHandler;
        }
        // Try to find the head, otherwise default to the documentElement
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else {
        // The jQuery version on the window is the one we want to use
        jQuery = window.jQuery;
        main();
    }
    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
        // Restore $ and window.jQuery to their previous values and store the
        // new jQuery in our local jQuery variable
        jQuery = window.jQuery.noConflict();
        // Call our main function
        main();
    }
    /******** Our main function ********/
    function main() {
        // Add some validation here to make sure UI is not loaded etc...
        jQuery.getScript('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js');
        jQuery(document).ready(function ($) {
            /******* Load CSS *******/
            var css_link = $("<link>", {
                rel: "stylesheet",
                type: "text/css",
                href: "StyleSheet.css"
            });
            css_link.appendTo('head');
            //loading plug in
            "use strict";
            $.fn.myplugin = function (options) {
                var settings = $.extend({
                    //default
                    isstatic: false
                });
                var options = $.extend(settings, options);
                var images = ['http://www.example1.com'/1.png, 'http://www.example2.com/final.gif'];
                //Iterate over the current set of matched elements
                return this.each(function () {
                    var obj = $('.div2');
                    obj.hide();
                    $(this).show();
                    $(this).click(function () {
                        obj.toggle("slow");
                        if (options.isstatic) {
                            $(".image-class").attr("src", images[0]);
                            options.isstatic = false;
                        }
                        else {
                            $(".image-class").attr("src", images[1]);
                            options.isstatic = true;
                        }
                    });
                });
                return options.isstatic = false;
            }

        });


    }
})();

我使用另一个文件。js

$(document).ready(function () {
    $('#mydiv').myplugin();
});

我在这里得到一个错误$是没有定义如何删除这个错误可能b我做了一些错误的jQuery变量或$在javascript文件

我猜你需要jquery进一步的脚本在您的页面或您的插件运行,所以jquery参考应该是第一个在头部,你可以尝试替换你的代码的一部分( document.getElementsByTagName("head")[0] || document.documentElement ).appendChild(script_tag)

var head=( document.getElementsByTagName("head")[0] || document.documentElement ); head.insertBefore(script_tag,head.firstChild);