没有定义从javascript获取错误$加载jquery库
loading jquery library from javascript getting error $ is not defined
我已经加载了我的jquery库在我的插件从javascript和得到一个错误$没有定义如果我从另一个javascript页面调用我的插件匿名函数以下是我的工作
(function () {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.11.3') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict();
// Call our main function
main();
}
/******** Our main function ********/
function main() {
// Add some validation here to make sure UI is not loaded etc...
jQuery.getScript('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js');
jQuery(document).ready(function ($) {
/******* Load CSS *******/
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "StyleSheet.css"
});
css_link.appendTo('head');
//loading plug in
"use strict";
$.fn.myplugin = function (options) {
var settings = $.extend({
//default
isstatic: false
});
var options = $.extend(settings, options);
var images = ['http://www.example1.com'/1.png, 'http://www.example2.com/final.gif'];
//Iterate over the current set of matched elements
return this.each(function () {
var obj = $('.div2');
obj.hide();
$(this).show();
$(this).click(function () {
obj.toggle("slow");
if (options.isstatic) {
$(".image-class").attr("src", images[0]);
options.isstatic = false;
}
else {
$(".image-class").attr("src", images[1]);
options.isstatic = true;
}
});
});
return options.isstatic = false;
}
});
}
})();
我使用另一个文件。js
$(document).ready(function () {
$('#mydiv').myplugin();
});
我在这里得到一个错误$是没有定义如何删除这个错误可能b我做了一些错误的jQuery变量或$在javascript文件
我猜你需要jquery进一步的脚本在您的页面或您的插件运行,所以jquery参考应该是第一个在头部,你可以尝试替换你的代码的一部分( document.getElementsByTagName("head")[0] || document.documentElement ).appendChild(script_tag)
var head=( document.getElementsByTagName("head")[0] || document.documentElement ); head.insertBefore(script_tag,head.firstChild);
相关文章:
- 使用javascript函数在页面初始化后加载jquery
- 什么'这是加载jQuery的最佳方式
- 在WordPress站点中加载jquery.js后加载javascript代码
- 延迟加载jquery后替换$(document).ready(function)
- 如何使用onLoad加载jQuery JSON.从Zipcode On Form获取城市和州
- 对页面重新加载 Jquery 执行操作
- 在Ajax(过滤器)加载之后加载jQuery脚本
- 异步加载JQuery时Tabbed Box已损坏
- 加载Jquery时Chrome扩展出错
- 在更改事件上加载jQuery DataTable
- 手动加载JQuery而不触发内容安全策略
- 如果更改,请在ng之后重新加载Jquery
- 脚本似乎在加载jQuery之前运行-未定义Uncaught ReferenceError:$
- 在使用jquery之前动态加载jquery
- 如何重新加载jquery,以便它能为下一个进程工作
- Firefox没有't从ajax.googleapis.com加载jquery
- 我无法在自定义Wordpress主题中成功加载jQuery
- 正在从maphilight href加载Jquery选项卡
- 如何在完全加载html之后加载jquery代码段
- 在页面加载jQuery/Javascript时,按.class对表行进行排序