jQuery就绪函数在WordPress中不起作用

11 wordpress

我在WordPress上使用jQuery(@the HOME页面),ready函数对我不起作用.我有一个index.php,它包括(php)页眉,页脚和侧边栏.我测试了这段代码:

<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>
Run Code Online (Sandbox Code Playgroud)

警报(带文字"test text")不会立即弹出!它只是在我的侧边栏加载后弹出.这意味着当我看到索引页面(侧栏未加载)时,我必须等待几秒钟,直到侧栏完成加载,然后才执行jQuery代码:警报弹出.所以准备好的功能不会起作用.任何人都可以告诉我为什么以及如何解决这个问题?谢谢.

squ*_*ndy 51

在WordPress环境中,使用:

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
}); 
Run Code Online (Sandbox Code Playgroud)


Arj*_*jan 1

警报(带有文本“测试文本”)只是没有立即弹出!它仅在我的站点栏加载后才会弹出。

正是使用ready. 当您希望它立即弹出时,只需执行以下操作

<script type="text/javascript">
  alert ("test text");
</script>
Run Code Online (Sandbox Code Playgroud)