PHP 文件没有读取我的 js 脚本文件

问题描述 投票:0回答:1

我试图确保我的 php 文件正在读取我的 js 文件,当我单击减少按钮时,我的控制台显示

Uncaught ReferenceError: decrease is not defined
onclick http://localhost:3000/index.php:1

我知道这表明当我单击不正确的按钮时我的 decrease() 函数不存在。我确保我也包含了 html 脚本标签。

这是我的两个文件

index.php

  <?php
  $price = 3;
  $quantity = 5;
  $total = $price * $quantity
?>

<!DOCTYPE html>
<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <p>Total: <?php echo $total; ?> </p>
    <button onclick='decrease();'>Click Me</button>
    <input type='text' id = 'num' value="0"></input>
    
    <script type='text/javascript' src='./index.js'></script>
  </body>
</html>

index.js

let i = null;

 
  function decrease(){
    i = i - 1;
    document.getElementById('num').value = i;
    console.log(i)
  }

我试过几次重启我的网络服务器,但都没有用。我错过了什么,它阻止它看到我的外部 js 脚本文件中的函数?

javascript php html apache
1个回答
0
投票

根据我在使用网络服务器时的理解,我不会为我的资源(如 js、CSS 或图像)使用相对路径。

如果您的目录如下所示:

Main
 |- index.php
 |- index.js

然后你可以像这样导入你的javascript文件而不带点

.

<script type='text/javascript' src='/index.js'></script>

有时当你从 Apache Netbean 创建项目时,项目 URL 会变成

http://localhost/ProjectName

例如,如果您从

index.php
访问您的
http://localhost/ProjectName/index.php
,那么您应该能够像这样获取 javascript 文件:

<script type='text/javascript' src='/ProjectName/index.js'></script>

我还没有测试过,希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.