【问题标题】:jQuery ready function doesn't work in WordPressjQuery 就绪功能在 WordPress 中不起作用
【发布时间】:2010-11-22 13:29:01
【问题描述】:

我在 WordPress(@the HOME 页面)上使用 jQuery,而准备功能对我不起作用。我有一个 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>

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

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    在 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
    }); 
    

    【讨论】:

    • 哇!谢谢!!!我花了几个小时试图弄清楚为什么我的 jQuery 不工作!
    • 顺便说一句:这确实回答了问题的通用标题,“jQuery 就绪功能在 wordpress 中不起作用”,但不能解决 “它只是弹出加载我的站点栏后启动”.
    【解决方案2】:

    加载侧边栏后会弹出警报,因为 ready() 应该在整个页面加载完成后执行。

    【讨论】:

      【解决方案3】:

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

      这正是使用readythe advantage。当您希望它立即弹出时,只需执行

      <script type="text/javascript">
        alert ("test text");
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-13
        • 2016-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-13
        • 1970-01-01
        相关资源
        最近更新 更多