jQuery is not loaded

jQuery is not loaded

本文关键字:loaded not is jQuery      更新时间:2023-09-26

我有这样的代码,但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的文章