【问题标题】:jquery is not working in wordpress and throws an error [duplicate]jquery在wordpress中不起作用并引发错误[重复]
【发布时间】:2021-11-25 04:29:39
【问题描述】:

我试图在我的存档页面上运行这个 jquery 代码,但它给了我一个错误“未定义的 $”。但它在 wordpress 之外工作。我不知道该怎么办,我很困惑。

$(".show-more-posts").on("click", () => {
    $(this).show();
 });

对不起,如果我的问题很愚蠢,我是新手。

【问题讨论】:

  • @j08691 感谢您的帮助。

标签: jquery wordpress


【解决方案1】:

您需要在 wordpress 中加载 jquery。在 functions.php 文件中使用此代码。

add_action('wp_enqueue_scripts', 'load_jquery_to_theme');

function load_jquery_to_theme(){
  wp_enqueue_script('jquery');
}

在你的 javascript 文件中使用这个

jQuery(document).ready(($) => {

  $(".show-more-posts").on("click", () => {
    $(this).show();
  });

});

【讨论】:

  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2021-01-11
  • 1970-01-01
相关资源
最近更新 更多