jQuery插件可通过控制台触发,但不能通过功能触发-为什么呢? [英] jQuery Plugins triggerable by Console, but not by function - why that?
问题描述
我有一个神秘的JS问题:我使用一个函数激活了不同的jQuery-Plugins.叫做这样:
I have a mysterious JS Problem: I activate different jQuery-Plugins with one function. It's called like this:
<script>
postAjaxCalls();
</script>
然后,相应的函数如下所示:
Then, the corresponding function looks like this:
function postAjaxCalls() {
jQuery("[title]").tooltip();
alert("this works great, tooltip not!");
jQuery("select").selectbox();
}
当我重新加载页面时,除工具提示插件外,其他所有东西都可以正常工作.现在,如果我将完全相同的代码发射到JS控制台中,则插件被激活:
When I reload the page, everything works but the tooltip plugin. Now, if I fire the exact same Code into the JS Console, the plugin is activated:
jQuery("[title]").tooltip();
为什么呢?为什么通过控制台激活却不能使用,而通过功能激活却不能使用?
Why that? Why does it work when activated via console, but doesn't work when activated via a function?
干杯!
推荐答案
在DOM准备就绪后,尝试在$(function() { .. })
中简称为$(function() { .. })
的代码来执行代码.
Try your code within $(document).ready(function() { .. })
in short $(function() { .. })
to execute your code after DOM ready.
jQuery(document).ready(function() {
function postAjaxCalls() {
jQuery("[title]").tooltip();
alert("this works great, tooltip not!");
jQuery("select").selectbox();
}
postAjaxCalls();
});
简而言之
jQuery(function() {
function postAjaxCalls() {
jQuery("[title]").tooltip();
alert("this works great, tooltip not!");
jQuery("select").selectbox();
}
postAjaxCalls();
});
这篇关于jQuery插件可通过控制台触发,但不能通过功能触发-为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!