我们知道,$
或jQuery
通常是整个jQuery库所在的JavaScript变量。
说是否有JavaScript代码:
function abc() {
$=1;
jQuery='foo';
$('p').hide(); //won't work
jQuery('p').show(); //won't work
}
在这种情况下;我们如何检查jQuery
或$
是否实际拥有有效的jQuery库?或者是否有一些其他值分配给这两个变量?我需要知道这个用于验证目的。应该有类似的东西:
if(typeof $==="jQuery")
有什么方法可以知道吗?
感谢。
答案 0 :(得分:4)
您可以查看the jquery
property:
if (jQuery && jQuery.fn && jQuery.fn.jquery) {
// Yep, it's jQuery
}
如果你需要允许jQuery可能根本没有加载的情况,你需要稍微修改一下:
if (typeof jQuery === "function" && jQuery.fn && jQuery.fn.jquery) {
// Yep, it's jQuery
}