简体   繁体   English

Chrome显示加载资源失败

[英]Chrome displaying Failed to load resource

I have a simple webpage structure: 我有一个简单的网页结构:

-index.html
-css
  -style.css

The contents of the html are: html的内容是:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>test</title>
  <link rel="stylesheet" href="/css/style.css">
</head>
<body>

</body>
</html>

When I open the html in Chrome I get the error: 当我在Chrome中打开html时,出现错误:

Failed to load resource file:///C:/css/style.css 无法加载资源文件:/// C:/css/style.css

My directory is in Mydocuments in Windows. 我的目录在Windows的Mydocuments中。 Why can't the .css file be found? 为什么找不到.css文件?

As the console shows, your path 如控制台所示,您的路径

/css/style.css

means "the 'css' directory in the root path": 表示“根路径中的'css'目录”:

C:/css/style.css

which is not what you want*. 这不是您想要的*。

Use css/style.css - this will mean "the 'css' sub-directory in the directory the current page's located in." 使用css/style.css这表示“当前页面所在目录中的'css'子目录”。

* But even if it had been what you want, access to local file:// URLs is limited in that any resources you request (images, style sheets) need to be in a sub-directory of the current page. *但是,即使它已经自己想要什么,访问本地file://网址是有限的,因为您请求的任何资源(图片,样式表)需要在当前页面的子目录。 Modern browsers impose this limitation for security reasons. 出于安全原因,现代浏览器强加了此限制。

Forward slash first means an absolute path (from the root). 首先,正斜杠表示绝对路径(从根开始)。 Since you are running your page from the file system, the root is the drive. 由于您是从文件系统运行页面的,因此根目录是驱动器。

No forward slash first means a relative path (from the current folder). 没有正斜杠首先表示相对路径(来自当前文件夹)。

You may also want to make a shortcut to your chrome browser that has the "--allow-file-access-from-files" flag, so you can better test your site locally as you add more advanced features. 您可能还想为带有“ --allow-file-access-from-files”标志的chrome浏览器创建快捷方式,以便在添加更多高级功能时更好地在本地测试您的网站。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM