首页 > 解决方案 > html页面中包含css和js时的相对路径问题

问题描述

我的服务器 web 目录结构就像

/www/site1/
    js/
    css/
    partial/
          -head.php
          -footer.php
    view/
         -page1.php
    -index.php

/www/site2/

head.html 包含 js 文件,例如

<script src="js/jquery.min.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/bootstrap.min.js"></script>

head.php 文件包含在 index.php 和 view/page1.php 中。

由于 js 文件的路径,它在 index.php 上运行良好,但在 page1.php 上运行良好。在 index.php 路径变得像

/www/site1/js/jquery.min.js

在 page1.php 路径变为

/www/site1/view/js/jquery.min.js

如何在 head.php 上包含 js,以便在包含在 index 和 page1 中时可以正常工作

标签: phphtmlweb

解决方案


尝试在源代码中添加斜杠

<script src="/js/jquery.min.js"></script>
<script src="/js/jquery-ui.js"></script>
<script src="/js/bootstrap.min.js"></script>

推荐阅读