jQuery 未加载

jQuery is not loaded

我有这样的代码,但是 console.log 没有带来任何东西,当我尝试在控制台中输入时,我可以看到它已加载,但是当我加载此脚本时,它不会给控制台带来任何东西jQuery 功能不起作用。代码如下:

var bbimagebannerfile = "/300x250.jpg";

document.write("<a href=\"%%__REDIRECT%%\" target=\"_blank\"><img src=\"" + bbimagebannerfile + "\" border=\"0\" /></a>");

if (typeof jQuery == 'undefined') {
    var headTag = document.getElementsByTagName("head")[0];
    var jqTag = document.createElement('script');
    jqTag.type = 'text/javascript';
    jqTag.src = '//Libraries/ThirdParty/Jquery/1.10.2.min.js';
    jqTag.onload = myJQueryCode;
    headTag.appendChild(jqTag);
}


console.log(window.jQuery); // Ar jau yra užkrautas jQuery

if ('undefined' !== typeof window.jQuery) {
    console.log(window.jQuery("#Tomas")); // Ar jau yra inicializuotas div'as su ID "Tomas" (wery bad approach)

    var main = function () {
        var t = $("#Tomas").offset().top;

        $(document).scroll(function () {
            if ($(this).scrollTop() > t)
            {
                $("#Tomas")
                        .css('position', 'fixed') //we change the position to fixed
                        .css('top', 0); // and the top to zero
            } else {
                $("#Tomas")
                        .css('position', 'static') //we change the position to fixed
                        .css('top', 0); // and the top to zero
            }
        });
    }

    $(document).ready(main);
}

如评论中 Sirko 所述,您已经参考了一些回调 myJQueryCode

我将仅展示此实现的示例:

var bbimagebannerfile = "/300x250.jpg";

document.write("<a href=\"%%__REDIRECT%%\" target=\"_blank\"><img src=\"" +
  bbimagebannerfile + "\" border=\"0\" /></a>");

var onJqueryLoad = function (flag) {
    if ('undefined' !== typeof window.jQuery) {
        console.log(window.jQuery("#Tomas")); // Ar jau yra inicializuotas div'as su ID "Tomas" (wery bad approach)

        var main = function () {
            var t = $("#Tomas").offset().top;

            $(document).scroll(function () {
                if ($(this).scrollTop() > t) {
                    $("#Tomas")
                      .css('position', 'fixed') //we change the position to fixed
                      .css('top', 0); // and the top to zero
                } else {
                    $("#Tomas")
                      .css('position', 'static') //we change the position to fixed
                      .css('top', 0); // and the top to zero
                }
           });
           if ('function' === typeof myJQueryCode && !flag) {
               myJQueryCode();    
           }
       }

       $(document).ready(main);
   }
}

if (typeof jQuery == 'undefined') {
    var headTag = document.getElementsByTagName("head")[0];
    var jqTag = document.createElement('script');
    jqTag.type = 'text/javascript';
    jqTag.src = '//Libraries/ThirdParty/Jquery/1.10.2.min.js';
    jqTag.onload = onJqueryLoad;
    headTag.appendChild(jqTag);
}

onJqueryLoad(true);

您也可以在 MDN

上查看有关 HTMLScriptElement 的文章