jQuery ready function doesn't work in WordPres

2019-01-14 17:29发布

I'm using jQuery at WordPress (@the HOME page) and the ready function doesn't work for me. I have a index.php which is including (php) a header, footer and a sidebar. I tested this code:

<script type="text/javascript" src="path_to_jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
   $(document).ready(function() {
      alert ("test text");
   });
</script>

The alert (with the text "test text") is just not popping up immediately! It's only popping up after my sidebar has been loaded. This means while I'm seeing the index page (sidebar is not loaded yet) I have to wait a few seconds until the sidebar has finished loading, and only then the jQuery code is executed: the alert is popping up. So the ready function just wont work. Can anybody tell me why and how I can solve this problem? Thanks.

标签: wordpress
3条回答
够拽才男人
2楼-- · 2019-01-14 18:10

The alert is popping up after the sidebar is loaded because ready() is supposed to be executed AFTER the whole page is done loading.

查看更多
Anthone
3楼-- · 2019-01-14 18:18

The alert (with the text "test text") is just not popping up immediately! It's only popping up after my sitebar has been loaded.

That's exactly the advantage of using ready. When you want it to popup right away, simply do

<script type="text/javascript">
  alert ("test text");
</script>
查看更多
Explosion°爆炸
4楼-- · 2019-01-14 18:19

within the WordPress environment, use this:

jQuery(function($){
    // now you can use jQuery code here with $ shortcut formatting
    // this executes immediately - before the page is finished loading
});


jQuery(document).ready(function($){
    // now you can use jQuery code here with $ shortcut formatting
    // this will execute after the document is fully loaded
    // anything that interacts with your html should go here
}); 
查看更多
登录 后发表回答