Javascript 测试 jQueryUI 是否已加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2260250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing if jQueryUI has loaded
提问by waiwai933
I'm trying to debug a website, and I think that jQueryUI may not have loaded properly. How can I test if jQueryUI has loaded?
我正在尝试调试一个网站,我认为 jQueryUI 可能没有正确加载。如何测试 jQueryUI 是否已加载?
回答by chrismacp
if (jQuery.ui) {
// UI loaded
}
OR
或者
if (typeof jQuery.ui != 'undefined') {
// UI loaded
}
Should do the trick
应该做的伎俩
回答by CMS
You need to check if both, the jQuery UI Libraryfile and CSS Themeare being loaded.
您需要检查是否正在加载jQuery UI库文件和CSS 主题。
jQuery UI creates properties on the jQuery object, you could check:
jQuery UI 在 jQuery 对象上创建属性,您可以检查:
jQuery.ui
jQuery.ui.version
To check if the necessary CSS file(s) are loaded, I would recommend you to use Firebug, and look for the theme files on the CSS tab.
要检查是否加载了必要的 CSS 文件,我建议您使用Firebug,并在 CSS 选项卡上查找主题文件。
I've seen problems before, when users load correctly the jQuery UI library but the CSS theme is missing.
我以前见过问题,当用户正确加载 jQuery UI 库但缺少 CSS 主题时。
回答by Mike
I know this is an old question, but here is a quick little script you can use to wrap all your jQuery UI things that don't have an associated event to make sure they get executed only after jQuery UI is loaded:
我知道这是一个老问题,但这里有一个快速的小脚本,您可以使用它来包装所有没有关联事件的 jQuery UI 内容,以确保它们仅在 jQuery UI 加载后才被执行:
function checkJqueryUI() {
if (typeof jQuery.ui != 'undefined') {
do_jqueryui();
}
else {
window.setTimeout( checkJqueryUI, 50 );
}
}
// Put all your jQuery UI stuff in this function
function do_jqueryui() {
// Example:
$( "#yourId" ).dialog();
}
checkJqueryUI();
回答by bdl
Just test for the ui object, e.g.
只需测试 ui 对象,例如
<script src="jquery.js"></script>
<script src="jquery-ui.js"></script>
<script>
$(function(){
// did the UI load?
console.log(jQuery.ui);
});
</script>
回答by Tushar Shukla
You can check if jQuery UI is loaded or not by many ways such as:
您可以通过多种方式检查 jQuery UI 是否已加载,例如:
if (typeof jQuery.ui == 'undefined') {
// jQuery UI IS NOT loaded, do stuff here.
}
OR
或者
if (typeof jQuery.ui != 'function') {
// jQuery UI IS NOT loaded, do stuff here.
}
OR
或者
if (jQuery.ui) {
// This will throw an error in STRICT MODE if jQuery UI is not loaded, so don't use if using strict mode
alert("jquery UI is loaded");
} else {
alert("Not loaded");
}

