【问题标题】:Can you insert jquery into a php file?你可以将jquery插入一个php文件吗?
【发布时间】:2019-12-10 12:09:56
【问题描述】:

我有以下 .php 文件:

<div class="excerpt">
  <?php get_the_excerpt();?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    $(".excerpt").hide();
  });
</script>

我想通过 jQuery 隐藏 div 类“摘录”。不幸的是,代码没有做任何事情,我不明白我哪里出错了。甚至可以在 php 中使用 jQuery 吗?

【问题讨论】:

  • 您的代码工作正常 - 正如您在我为您的问题编辑的 sn-p 中看到的那样。如果它不适合您,请检查控制台是否有错误
  • 它应该可以工作。
  • 即使我们没有摘录,我也用简单的 echo 替换了那行,效果很好!
  • @RoryMcCrossan 没有错误信息,只是没有隐藏内容,我不太明白为什么。

标签: php jquery html wordpress


【解决方案1】:

标识符$通常不会在像cms这样的wordpress中默认启用

你必须使用标识符jQuery

为了使标识符$工作,你可以试试这个

(function($){
    // inside this scope, $ can be used
    $(document).ready(function(){

        // other scripts

    });
})(jQuery);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多