0

我正在尝试仅使用 html、css 和一些 jquery\javascript 来制作一个可在线和离线浏览的小网站。因此,我使用所有相对路径并且一切正常,除非我遇到问题在我的所有页面中加载自定义菜单,并包含一些智能 jquery。

但是,由于我的 menu.html 加载在位于树结构的不同子目录中的不同页面中,我想知道在菜单中写下不同声音的 href 链接的最聪明的方法是什么。

我最初开始使用 menu.html 中的所有绝对路径,但当然它只能在线离线工作,具体取决于我在绝对路径中使用的根域(http://mywebsite.com/或 file:/// D:我的文件夹/等)。

当然,在链接开头使用 / 也只能在线使用,因为在本地 / 代表网站文件夹所在的驱动器号,当且仅当网站的文件夹保存在最高路径中时它才会起作用,例如作为 D:/myWenbsite。无论本地路径如何,我都想做一些更具适应性的东西。

4

2 回答 2

2

The best way in my opinion is to use relative URL's from the root. For example in your menu.html file when you reference jquery you can do the following:

/javascript/jquery.min.js

Adding the beginning '/' makes it so that the path always starts from the root of the domain no matter where your html is at in your directory.

If you used:

javascript/jquery.min.js

That means in whatever directory your menu.html file is in, a folder for javascript would also need to exist and that is not generally wanted.

于 2015-05-06T15:16:08.840 回答
0

在一个小脚本中使用<base>命令来改变它解决了我的问题。

这是一个例子:

<head>
<!-- Here a direct path is need to firstly load jquery -->
    <script type = "text/javascript" src = "../include/js/jquery-1.10.2.min.js"></script>
    <base id="host" href="" />
    <script>
        /* Where am I? */
        here = window.location.href;
        hereIndex = here.indexOf('temp-test');

        /* make substring from root till temp-test/ */
        newPathname = here.substring(0, hereIndex+10); //+10 to consdier also temp-test/

        $("#host").attr("href", newPathname);
    </script>
</head>

不知道有没有更好的办法。

无论如何,即使页面在控制台日志中正确呈现,我仍然会在我拥有的每个相对路径上遇到错误, GET file:///D:/temp-test/core/image/temp1.jpg net::ERR_FILE_NOT_FOUND但是,例如,此图像已正确加载。那么基本标签是怎么回事?这有点不被认可,但它有效..

我需要进一步调查。

于 2015-05-06T18:50:18.983 回答