Bootstrap的JavaScript需要jQuery,而jQuery已加载
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Bootstrap的JavaScript需要jQuery,而jQuery已加载,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2069字,纯文字阅读大概需要3分钟。
内容图文
我在加载Bootstrap库时遇到错误,因为它总是会出现此错误:
Uncaught Error: Bootstrap’s JavaScript requires jQuery
尽管如此,在确保已加载jQuery但仍出现错误之后,我仍附加了Bootstrap库.
我正在使用以下代码通过创建元素并将jQuery附加到页面并将其附加到文档:
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '3.1.1') {
console.log("jQuery LOADED");
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js");
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
console.log(window.jQuery.fn.jquery);
scriptLoadHandler();
}
};
} else {
console.log("ONLOAD STATE");
script_tag.onload = scriptLoadHandler;
}
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
function scriptLoadHandler() {
// Restore $and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main();
}
准备好文档后,在这里创建Bootstrap:
function main() {
jQuery(document).ready(function ($) {
var bootstrap_script = document.createElement('script');
bootstrap_script.setAttribute("type", "text/javascript");
bootstrap_script.setAttribute("src",
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js");
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(bootstrap_script);
})
}
只是为了阐明一件重要的事情
我在一个单独的js文件中编写js代码,因为我想以后用作jQuery插件.因此,我尝试附加库,因为我不保证目标页面会包含或不包含这些库
解决方法:
尝试使用https加载jquery
script_tag.setAttribute("src",
"https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js");
希望它能工作.
内容总结
以上是互联网集市为您收集整理的Bootstrap的JavaScript需要jQuery,而jQuery已加载全部内容,希望文章能够帮你解决Bootstrap的JavaScript需要jQuery,而jQuery已加载所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。