我正在从 Morten Rand-Hendriksen 的课程(JavaScript 基本培训)中学习 JavaScript。我下载了所有练习文件并使用本地运行的 Firefox 和 VSCode 以及 Live Server,我尝试在浏览器中加载下面的示例代码(示例 5.02)。
它不显示显示背包图片的 CSS 样式。在控制台中,我收到此错误:
The resource from “http://127.0.0.1:5500/assets/style.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
我在“设置”中将“跟踪内容”设置为“关闭”(参见屏幕截图),但没有帮助。
我确认文件存在“..\assets\images\everyday.svg”
更新:我试图直接在 Firefox 中加载它,而不是 LiveServer。我在控制台中看到,Cross-Origin Request Blocked: The Same Origin Policy disallows read the remote resource at file:///C:/Users/Me/Documents/Learn/JS/05_03/Backpack.js。(原因:CORS 请求不是 http)。
实际结果
预期结果:
代码在这里:
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BackpackPacker</title>
<link
href="https://fonts.googleapis.com/css2?family=Oswald:wght@200..700&family=Work+Sans:wght@100..900&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="../assets/style.css"
type="text/css"
media="all"
/>
<script type="module" src="Backpack.js"></script>
<script type="module" src="script.js"></script>
</head>
<body>
<header class="siteheader">
<div class="site-title">BackpackPacker</div>
<div class="site-description">All backpack packing, all the time.</div>
</header>
<main class="maincontent"></main>
<footer class="sitefooter">
<p>
Demo project for JavaScript Essential Training, a LinkedIn Learning
course.
</p>
</footer>
</body>
</html>
背包.js
class Backpack {
constructor(
name,
volume,
color,
pocketNum,
strapLengthL,
strapLengthR,
lidOpen,
dateAcquired,
image
) {
this.name = name;
this.volume = volume;
this.color = color;
this.pocketNum = pocketNum;
this.strapLength = {
left: strapLengthL,
right: strapLengthR,
};
this.lidOpen = lidOpen;
this.dateAcquired = dateAcquired;
this.image = image;
}
toggleLid(lidStatus) {
this.lidOpen = lidStatus;
}
newStrapLength(lengthLeft, lengthRight) {
this.strapLength.left = lengthLeft;
this.strapLength.right = lengthRight;
}
backpackAge() {
let now = new Date();
let acquired = new Date(this.dateAcquired);
let elapsed = now - acquired; // elapsed time in milliseconds
let daysSinceAcquired = Math.floor(elapsed / (1000 * 3600 * 24));
return daysSinceAcquired;
}
}
export default Backpack;
脚本.js
/**
* Traverse the DOM tree using querySelector() and querySelectorAll()
* @link https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector
* @link https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll
*/
import Backpack from "./Backpack.js";
const everydayPack = new Backpack(
"Everyday Backpack",
30,
"grey",
15,
26,
26,
false,
"December 5, 2018 15:00:00 PST",
"../assets/images/everyday.svg"
);
const main = document.querySelector(".maincontent");
const content = `
<article class="backpack" id="everyday">
<figure class="backpack__image">
<img src=${everydayPack.image} alt="" />
</figure>
<h1 class="backpack__name">${everydayPack.name}</h1>
<ul class="backpack__features">
<li class="backpack__volume">Volume:<span> ${
everydayPack.volume
}l</span></li>
<li class="backpack__color">Color:<span> ${everydayPack.color}</span></li>
<li class="backpack__age">Age:<span> ${everydayPack.backpackAge()} days old</span></li>
<li class="backpack__pockets">Number of pockets:<span> ${
everydayPack.pocketNum
}</span></li>
<li class="backpack__strap">Left strap length:<span> ${
everydayPack.strapLength.left
} inches</span></li>
<li class="backpack__strap">Right strap length:<span> ${
everydayPack.strapLength.right
} inches</span></li>
<li class="backpack__lid">Lid status:<span> ${
everydayPack.lidOpen
}</span></li>
</ul>
</article>
`;
main.innerHTML = content;
../assets/style.css 中的 CSS 文件
body {
margin: 0;
font-family: "Work Sans", sans-serif;
font-size: 1.6rem;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: "Oswald", sans-serif;
}
figure {
margin: 0;
}
.siteheader {
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem;
}
.site-title {
margin-top: 1rem;
margin-bottom: 1rem;
font-family: "Oswald", sans-serif;
font-size: 5rem;
font-weight: 200;
}
.siteheader:after {
display: block;
margin: 4rem 0;
content: "⚍⚌⚍";
}
.maincontent {
margin: 0 auto;
padding: 0 1rem;
max-width: 60rem;
}
.page-header {
margin-bottom: 3rem;
}
.page-header__heading {
margin-top: -1rem;
text-align: center;
}
.page-header:after {
margin: 4rem auto;
display: block;
content: "";
border-bottom: 3px solid black;
width: 4rem;
}
.backpack {
margin-bottom: 4rem;
/* padding-bottom: rem; */
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto 1fr;
gap: 0 3rem;
}
.backpack:after {
margin: 0 auto;
padding-top: 4rem;
display: block;
grid-column: span 2;
content: "";
border-bottom: 3px solid black;
width: 4rem;
}
.backpack__image {
grid-row: span 2;
}
.backpack__name {
margin-top: 2rem;
margin-bottom: 0;
padding-bottom: 2rem;
font-size: 5rem;
line-height: 1;
text-transform: uppercase;
border-bottom: 3px solid black;
}
.backpack__features {
margin: 0;
padding: 2rem 0 0 0;
list-style-type: none;
}
.backpack__features li {
padding: 0.2rem 0;
}
/* Subgrid. See https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Subgrid */
@supports (grid-template-columns: subgrid) {
.backpack__features {
display: grid;
grid-template-columns: auto 1fr;
gap: 0 1rem;
}
.backpack__features li {
display: grid;
grid-column: span 2;
grid-template-columns: subgrid;
}
}
.lid-toggle {
display: inline-block;
border: 3px solid black;
border-radius: 0.3rem;
padding: 2rem;
font-size: 1.6rem;
cursor: pointer;
background-color: white;
}
.lid-toggle:hover,
.lid-toggle:focus {
background-color: hsl(0, 0%, 95%);
}
.lid-toggle:active {
background-color: black;
color: white;
}
.sitefooter {
margin-top: 8rem;
padding: 2rem;
display: flex;
justify-content: center;
background: black;
color: white;
}