简体   繁体   English

HTML文件路径上是否需要“ ./”和“ ../”?

[英]Do I need “./” and “../” on HTML files paths?

I'm using relative URL in attributes like href and src . 我在hrefsrc等属性中使用相对URL。 Do I really need to use ./ and ../ before the paths to correctly establish the link? 在路径正确建立链接之前,我真的需要使用./../吗?

When I'm done coding index.html, I copy the code and use it on others .html files of my project. 完成index.html的编码后,我将代码复制并在项目的其他.html文件中使用。 I find it frustrating having to correct every path then. 我发现必须纠正每条路径而感到沮丧。

My projects folder structure is like: 我的项目文件夹结构如下:

website-project folder
      index.html

      css folder
          style.css

      img folder
           image.png

      about folder
          about.html

If I use index.html code to start about.html I need to change every path begging from ./ to ../ . 如果我使用index.html代码启动about.html ,则需要将乞求的每个路径都从./更改为../ Using "Find & Replace" tool makes it easier but still it would be nicer if I could link to paths based on the root folder and not the present folder. 使用“查找和替换”工具可以使它更容易,但是如果我可以基于根文件夹而不是当前文件夹链接到路径,那还是更好。 Ex: 例如:

Using 使用

href="website-project/img/image.png"

inside about.html would link to website-project/img/image.png instead of website-project/about/img/image.png which leads to an error. about.html内部将链接到website-project/img/image.png而不是website-project/about/img/image.png ,这会导致错误。

UPDATE JULY 07 2016 2016年7月7日更新

Inside index.html page if I make a <a> with href="/about/about.html instead of linking to: index.html页面中,如果我使用href="/about/about.html而不是链接到<a>进行链接:

file:///home/guizo/Documents/website-project/about.html

it links to: 它链接到:

file:///about/about.html

The same goes on for the others links: 其他链接也是如此:

Try using 尝试使用

href="/img/image.png"

/ refers to your root directory. /指您的根目录。

EDIT: The above will not work for local static websites. 编辑:以上不适用于本地静态网站。 In these cases, you should try using the <BASE href /> tag which tags links on to a fixed base address. 在这些情况下,您应该尝试使用<BASE href />标记,该标记链接到固定的基地址。 It can be used as follows: 可以如下使用:

For index.html : 对于index.html

<html>
    <head>
        <title>index</title>
        <BASE href="file:///home/guizo/Documents/website-project/" />
    </head>
    <body>
        <a href="about/about.html">Go to about</a>
    </body>
</html>

For about.html : 对于about.html

<html>
    <head>
        <title>About</title>
        <BASE href="file:///home/guizo/Documents/website-project/" />
    </head>
    <body>
        <a href="index.html">Go to index</a>
    </body>
</html>

try this '/img/image.png'. 试试这个“ /img/image.png”。 / represents root of the site. /代表网站的根目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 html 忽略文件路径 - html ignores files paths HTML和CSS文件中的URL路径 - URL Paths in HTML and CSS files 我需要将这些视口元标记放在html和css文件中的哪里? - Where do I need to place these viewport meta tags in my html and css files? 我是否需要在cordova / phonegap项目中的所有html文件中添加app.initialize() - Do I need to add app.initialize() in all my html files in a cordova/phonegap project 我是否需要在 html 文件中添加字体(与引导文件一起的文件)文件? - Do I need to add Fonts(A file which is along with Bootstrap files) file in html file? 如果我在 index.php 文件中连接到 mysql 数据库,是否需要在任何带有嵌入式 php 的链接 html 文件中再次执行此操作? - If I connect to a mysql database in my index.php file, do I need to do it again in any linked html files with embedded php? 我需要一个不同的html页面进行打印吗? - Do I need a different html page for printing? [HTML / PHP]:是否需要更改.html文件的扩展名 - [HTML/PHP]: Do I need to change the extension of .html file 我需要总是指定开口吗<html> HTML5 中的标签? - Do I need to always specify the opening <html> tag in HTML5? 我需要` <html> `用于使用Swift发送HTML电子邮件 - Do I need `<html>` for sending HTML email using Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM