【发布时间】:2020-06-01 13:00:31
【问题描述】:
所以,这个问题是基于我目前正在做的一项家庭作业。我得到了一个 HTML 文档和两个样式表。 HTML 文档真的很简单:
<html>
<head>
<title>DOM-scripting: change style</title>
<link rel="stylesheet" href="style/style1.css" id="stylesheet"/>
</head>
<body>
<nav class="chooseStyle">
<input type="button" id="style1Button" value="Style 1"/>
<input type="button" id="style2Button" value="Style 2"/>
</nav>
<article>
<h1>Style change using DOM-scripting</h1>
<p> sample text </p>
<p> sample text </p>
<p> sample text </p>
</article>
<nav class="links">
<ul>
<li><a href="link">school name</a></li>
<li><a href="link">school name</a></li>
</nav>
<script src="scriptToUse.js"></script>
</body>
</html>
style1.css 包含以下内容:
body {
background-color: #676632;
}
input[type="button"] {
color: #F5F265;
background-color: #9B9302;
}
article {
margin-left: 150px;
margin-right: 150px;
text-indent: 20px;
color: #C9C892;
}
h1 {
color: #EEF1BA;
text-align: center;
}
a {
color: #A1FA56;
text-decoration: none;
font-weight: bold;
}
ul {
padding: 0;
}
li {
list-style-type: none;
}
Style2.css 包含以下内容:
body {
color: #EEEEAA;
}
input[type="button"] {
color: #9B9302;
background-color: #F5F265;
}
article {
margin-left: auto;
margin-right: auto;
text-align: justify;
color: #A3A163;
}
h1 {
color: #AAAA66;
text-align: left;
}
a {
color: #CDCF9B;
}
ul {
padding: 0;
}
li {
list-style-type: none;
}
练习的第一部分说明我需要创建一个 javascript 文件,该文件允许网页的样式表根据单击的按钮进行更改。第一个按钮引用第一个样式表,第二个按钮引用第二个样式表。我发现这很容易,一切正常。
但是,练习的第二部分陈述如下:“现在编辑 javascript 文件,以便在单击按钮时只更改文章的样式。不要编辑 CSS 文件。”这意味着样式表的 href 仍应更改,就像第一部分一样,但现在它可能只影响文章元素的样式,而不是整个页面的样式。
过去一个小时我一直在寻找相关问题,但似乎没有什么能真正提供解决方案。我已经阅读了一些问题,指出不可能仅将样式表应用于特定元素而不更改样式表本身的任何内容。请记住,我才刚上大学一年级,才刚刚开始学习 javascript,这意味着解决方案不应该那么难找到。我猜?
到目前为止我写的 Javascript 代码:
'use strict';
const handleLoad = () => {
let style1 = document.getElementById('style1Button');
let style2 = document.getElementById('style2Button');
style1.addEventListener('click', handleClick);
style2.addEventListener('click', handleClick);
};
const handleClick = (event) => {
if(event.target.value === "Style 1") {
document.getElementById("stylesheet").href = "style/style1.css";
// should only change article style
// currently still referring to the whole document
} else {
document.getElementById("stylesheet").href = "style/style2.css";
// should also only change article style
// also still referring to the whole document
}
};
window.addEventListener('load', handleLoad);
有什么方法可以阻止除文章元素之外的所有元素的 CSS 规则吗?或者可能更改不应该受到影响的元素的类或 ID?
提前非常感谢:)
这就是最终解决它的方法。 (代码可能不像它应该的那样紧凑和正确,但它可以工作,所以我非常高兴:D):
'use strict';
const handleLoad = () => {
let style1 = document.getElementById('style1Button');
let style2 = document.getElementById('style2Button');
style1.addEventListener('click', handleClick);
style2.addEventListener('click', handleClick);
makeShadow();
};
const handleClick = (event) => {
let article = document.querySelector('article');
let shadow = article.shadowRoot;
let style = event.target.value === "Style 1" ? "style/style2.css" : "style/style1.css";
for(let node of shadow.childNodes) {
if(node.hasAttribute('href')){
node.href = style;
}
}
};
const makeShadow = () => {
let article = document.querySelector('article');
let articleClone = makeClone(article);
let shadow = article.attachShadow({mode:'open'});
let link = document.createElement('link');
link.rel = "stylesheet";
link.href = 'style/style1.css';
shadow.appendChild(articleClone);
shadow.prepend(link);
};
const makeClone = (article) => {
let clone = article.cloneNode();
clone.innerHTML = article.innerHTML;
return clone;
};
window.addEventListener('load', handleLoad);
【问题讨论】:
-
你能提供你的javascript吗?我们不在这里做你的功课,但如果你卡在脚本的某个部分,我们很乐意提供帮助。
-
@ArnoTenkink 当然!我当然不希望你做我的作业:p,但就像你说的那样,我真的被困在这部分,我只是想知道如何解决它。
-
那些 css 文件是什么样的?全局添加样式表并希望它只影响某些元素有点困难。如果选择器是命名空间,那么它很容易完成,我们只需要打破除
article之外的所有内容的命名空间,如果不是,那么你的脚本会变大 -
我将文件添加到原始问题中。 CSS 文件仅包含对元素标记名称的引用。
标签: javascript html css stylesheet