wordpress - jQuery 就绪功能在 WordPress 中不起作用

标签 wordpress

我在 WordPress(@the HOME 页面)上使用 jQuery,而 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>

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

最佳答案

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

关于wordpress - jQuery 就绪功能在 WordPress 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1327085/

相关文章:

javascript - jQuery ajax 停止工作

wordpress - 无法安装 bbpress 主题

php - 页面加载完成后,移动浏览器的图像被隐藏

php - 当 Woocommerce 中的产品缺货时显示 "out of stock"标签

javascript - 我正在尝试让我的导航菜单向左滑动并用它插入正文

html - 左侧边栏被浏览器窗口截断

php - 从 Woocommerce 中的产品 ID 列出产品类别层次结构

php - Wordpress - 以编程方式添加不生成缩略图的产品

php - 显示 Woocommerce 中选定产品变体的特定数据

javascript - 如何将 jquery 调整大小功能添加到 WP 谷歌地图插件?