Javascript jQuery未加载
我有这样的代码,但是console.log没有带来任何东西,当我尝试在控制台中键入时,我可以看到它被加载了,但是当我加载这个脚本时,它没有给控制台带来任何东西,jQuery函数不起作用。代码如下:Javascript jQuery未加载,javascript,jquery,scroll,Javascript,Jquery,Scroll,我有这样的代码,但是console.log没有带来任何东西,当我尝试在控制台中键入时,我可以看到它被加载了,但是当我加载这个脚本时,它没有给控制台带来任何东西,jQuery函数不起作用。代码如下: var bbimagebannerfile = "/300x250.jpg"; document.write("<a href=\"%%__REDIRECT%%\" target=\"_blank\"><img src=\"" + bbimagebannerfile + "\" b
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);
}
正如注释中提到的,您已经有了对一些回调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的文章,展示如何包括jQuery和脚本。可能您在jQuery之前包含了您的脚本。似乎触发很好,一定是库链接。脚本加载是异步的。好的,您可以尝试通过插入一个新标记来加载jQuery,但是在访问jQuery对象之前,您不会等待加载完成。网页上放置了javascript,其中显示了横幅。在本例中,javascript加载了这段代码,我已经粘贴了这段代码,但它没有在页面上显示console.log。当我在本地尝试时,一切都很好。@Sirko我怎么能解决这个问题?如何检查库是否已完成加载,然后启动函数的执行?非常感谢您的帮助。这很有魅力。另外,只有当用户向下滚动并到达div时,才能加载该文件吗?只有在到达横幅文件后,横幅文件才会加载并启动jQuery脚本?在延迟加载横幅的情况下,我建议使用类似于小提琴的方法:是否可以将两个脚本组合起来。我是说可滚动div和延迟加载?这就像当用户滚动到该点时,脚本加载,然后变成可滚动的?