问题
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();
}
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?
Cheers!
回答1:
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();
});
OR in short
jQuery(function() {
function postAjaxCalls() {
jQuery("[title]").tooltip();
alert("this works great, tooltip not!");
jQuery("select").selectbox();
}
postAjaxCalls();
});
回答2:
You are probably calling postAjaxCalls
before the DOM is ready. When you call it from the console, the DOM is ready, so it works.
Try this:
$(function(){
postAjaxCalls();
});
来源:https://stackoverflow.com/questions/11545467/jquery-plugins-triggerable-by-console-but-not-by-function-why-that