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

时间:2015-08-08 18:43:18

标签: javascript jquery jquery-plugins

如果我从另一个javscript页面调用我的插件匿名函数,我已经从javascript加载了我的jquery库并且出现了错误 $未定义 以下是我的工作

(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;
            }



        });




    }
})();

我使用另一个file.js

来调用它
$(document).ready(function () {
    $('#mydiv').myplugin();

});

我在这里收到错误 $未定义如何删除此错误可能b我在jQuery变量或$ javascript文件中做错了

1 个答案:

答案 0 :(得分:2)

我猜你的页面中有更多脚本需要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);