Javascript 在PHP页面中加载jQuery-无头标记

Javascript 在PHP页面中加载jQuery-无头标记,javascript,php,jquery,html,wordpress,Javascript,Php,Jquery,Html,Wordpress,这可能是一个非常简单的答案,但我就是想不出来 基本上,我只有一个以打开开始的php页面,您可以将javascript放在页面底部的php标记之外。但是,当您在网页中使用php时,应该添加html和head和body标记。一个简单的php页面可能如下所示: <html> <head> <!-- stylesheets and javascript --> </head> <body> <?php //You php-

这可能是一个非常简单的答案,但我就是想不出来


基本上,我只有一个以打开开始的php页面,您可以将javascript放在页面底部的php标记之外。但是,当您在网页中使用php时,应该添加html和head和body标记。一个简单的php页面可能如下所示:

<html>
<head>
  <!-- stylesheets and javascript -->
</head>
<body>
  <?php
    //You php-code
  ?>
  <!-- scripts at the end of the page to let the side load faster -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script type="text/javascript">
   $('input[name="_manage_item"]').click(function() {
     $('input[name="_item"]')[this.checked ? "show" : "hide"]();
   });
</script>
</body>
</html>
依靠你的wordpress标签,我建议你阅读。这里描述了wordpress中有关javascript使用的所有细节。可能最常用的方法是使用函数

向WordPress生成的页面和WordPress主题或插件添加JavaScript的安全推荐方法是使用wp_enqueue_脚本


我还建议您将代码合并到一个不同的文件中,并直接在jquery库之后加载它。这是一个很好的实践,因为这样浏览器可以缓存您的脚本,并且不会在每个页面请求中下载它。您还应该将它包装到一个函数中,以确保它仅在文档处于就绪状态时执行

请分享您的完整代码。我假设这只是插入到完整HTML页面中的HTML代码。Wordpress应该已经加载了jQuery库。也许$没有定义,您需要使用jQuery。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<html>
<head>
  <!-- stylesheets and javascript -->
</head>
<body>
  <?php
    //You php-code
  ?>
  <!-- scripts at the end of the page to let the side load faster -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script type="text/javascript">
   $('input[name="_manage_item"]').click(function() {
     $('input[name="_item"]')[this.checked ? "show" : "hide"]();
   });
</script>
</body>
</html>