如何在不更改文本的情况下使用JavaScript更改页眉的颜色(我使用的是innerHTML)



例如,当我使用选择菜单并选择紫色时。我希望页眉的颜色更改为该颜色,但不希望文本更改。innerHTML正在更改文本和颜色。我该如何着手解决这个问题?

#header {
color: green;
}
#list {
margin-top: 20px;
}
.purple-color{
color: purple;
}
</style>
</head>
<body>
<h1 id="header">Header</h1>
<!--Change Value of Header with Input Field-->
<div>
Name:<input type="text" id="my-text" value="">
<p>Click the button to change the value of the header.</p>
<button onclick="onChange('header')">Click Me</button>
</div>
<!-- Select Size Menu-->
<div class= "select-menu">
<label for="header-color">Color:</label>
<select name="select-menu" id = "list" onchange="changeColor()" >
<option value="purple">Purple</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
</div>
<script>
function changeColor(){
const color = document.getElementById("header");
const selectColor = document.getElementById('list').value;

if (selectColor === "purple") {
color.innerHTML = selectColor;
color.style = 'color: #F00';
}
else if (selectColor === "blue") {
color.innerHTML = selectColor;
color.style = 'color: #F00';
}
else if (selectColor === "green") {
color.innerHTML = selectColor;
color.style = 'color: #F00';
}

}
</script>

将表单值设置为要设置的颜色可能更有效。然后你可以让你的changeColor()函数根据selectColor设置颜色。应使用color.style.color而不是color.style设置文本颜色。

此外,changeColor()函数不应该更改标头的innerHTML。从你的评论来看,这似乎应该是对点击我按钮的回应。

这个代码应该做你想做的事:

<html>
<head>
<style type="text/css">
#header {
color: green;
}
#list {
margin-top: 20px;
}
</style>
</head>

<body>
<h1 id="header">Header</h1>
<!--Change Value of Header with Input Field-->
<div>
Name:<input type="text" id="my-text" value="">
<p>Click the button to change the value of the header.</p>
<button onclick="changeText('header')">Click Me</button>
</div>

<!-- Select Size Menu-->
<div class= "select-menu">
<label for="header-color">Color:</label>
<select name="select-menu" id = "list" onchange="changeColor()" >
<option value="purple">Purple</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
</div>

<script>
function changeColor(){
const color = document.getElementById("header");
const selectColor = document.getElementById("list").value;
color.style.color = selectColor;

}
function changeText(id) {
const elem = document.getElementById(id);
const text = document.getElementById("my-text").value;
elem.innerHTML = text;
}
</script>
</body>
</html>

修改后的脚本标签:

<script>
function changeColor(){
const color = document.getElementById("header");
const selectColor = document.getElementById('list').value;
if (selectColor === "purple") {
color.setAttribute('style', 'color: purple')
}
else if (selectColor === "blue") {
color.setAttribute('style', 'color: blue')
}
else if (selectColor === "green") {
color.setAttribute('style', 'color: green')
}
// You can also use something like this
// color.setAttribute('style', `color: ${selectColor}`); // selectColor has to be a valid css color value

}
</script>

设置innerHTML会替换元素的HTML内容,在您的情况下是它的文本。只需单独使用color.style.color即可。请注意,我使用的是color.style.color,而不是color.style——您正在将整个样式对象重新分配给一个字符串!

function changeColor() {
const color = document.getElementById("header");
const selectColor = document.getElementById('list').value;

if (selectColor === "purple") {
// color.innerHTML = selectColor;
color.style.color = selectColor
} else if (selectColor === "blue") {
// color.innerHTML = selectColor;
color.style.color = selectColor;
} else if (selectColor === "green") {
// color.innerHTML = selectColor;
color.style.color = selectColor;
}
}
#header {
color: green;
}
#list {
margin-top: 20px;
}
.purple-color {
color: purple;
}
</style></head><body>
<h1 id="header">Header</h1>
<!--Change Value of Header with Input Field-->
<div>
Name:<input type="text" id="my-text" value="">
<p>Click the button to change the color of the header.</p>
<button onclick="changeColor()">Click Me</button>
</div>
<!-- Select Size Menu-->
<div class="select-menu">
<label for="header-color">Color:</label>
<select name="select-menu" id="list" onchange="changeColor()">
<option value="purple">Purple</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
</div>

你在这里的困惑可能是一个很好的提醒,要小心你所命名的东西。为什么不将标题元素命名为header或类似的名称?这样,当人们阅读你的代码时,他们会看到一些更能说明正在发生的事情的东西,比如header.style.color = "purple",而不是像color.style.color = "purple"那样令人困惑的东西。

最新更新