使用fancybox v2时,错误window.jQuery未定义?

时间:2012-03-05 04:32:13

标签: jquery fancybox

从链接fancybox

使用fancybox时出错
<script>
var yourock = {};
(function() {
    var attempts = 30;
    var old_jQuery;
    if (typeof(jQuery) != "undefined") {
        if (typeof(jQuery.noConflict) == "function") {
            old_jQuery = jQuery;
            delete jQuery;
        }
    }

    var addLibs = function() {
        var head = document.getElementsByTagName("head");
        if (head.length == 0) {
            if (attempts-- > 0) setTimeout(addLibs, 100);
            return;
        }

        var node = document.createElement("script");
        node.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
        head[0].appendChild(node);
        checkLibs();
    }

    var checkLibs = function() {
        if (typeof(jQuery) == "undefined" || typeof(jQuery) != "function" || jQuery("*") === null) {
            if (attempts-- > 0) setTimeout(checkLibs, 100);
            return;
        }
        yourock.jQuery = jQuery.noConflict(true);
        if (typeof old_jQuery == "undefined")
            jQuery = old_jQuery;
    }
    addLibs();

    var jQuery;

    var fancybox = document.createElement("script");
    fancybox.setAttribute("type","text/javascript");
    fancybox.setAttribute("src",
        "/fancybox/jquery.fancybox.js");
    fancybox.onload = scriptLoadHandler;
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(fancybox);

    function scriptLoadHandler() {
        jQuery = window.jQuery.noConflict(true);
        main();
    }
    function main() { 
        jQuery(document).ready(function($) {
            var css_link = $("<link>", { 
                rel: "stylesheet", 
                type: "text/css", 
                href: "/fancybox/fancybox.css" 
            });
            css_link.appendTo("head");
            $("#fancybox-manual-b").click(function(){
                $.fancybox.open({
                    href : "iframe.html",
                    type : "iframe",
                    padding : 5
                });
            });
        });
    }
})();
</script>

和html:

<a id="fancybox-manual-b" href="javascript:;">Open single item, custom options</a>

当我运行代码时,错误是:window.jQuery is undefined$ is not a function,如何修复

0 个答案:

没有答案