我在全局 JS 文件中有此代码。
$(document).ready(function() {
DoStuff();
});
不幸的是,有些页面包含此文件但不包含 jquery 文件。所以我收到错误:
Message: The value of the property '$' is null or undefined,
not a Function object
仅在加载 JQuery 文件时如何运行 document.ready()
。
最佳答案
if(window.jQuery) {
$(document).ready(function() {
DoStuff();
});
}
更新
检查window.jQuery
,而不仅仅是jQuery
。这应该可以避免“未定义”错误。
关于jquery - 仅当加载 jquery 时才运行 document.ready,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10356120/