如何使用 getElementById("").style.color 在 JavaScript 中更改对象的颜色
How do I change the color of an object with getElementById("").style.color in JavaScript
我正在尝试在单击按钮 2(名为“蓝色”)时将框的颜色更改为蓝色。
这是无效的代码....有人能帮忙吗?
演示
document.getElementById("button1").addEventListener("click", function(){
document.getElementById("box").style.height = "300px";
document.getElementById("box").style.width = "300px";
});
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.color = 'blue';
});
<!DOCTYPE html>
<html>
<head>
<title>Jiggle Into JavaScript</title>
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> -->
</head>
<body>
<p>Press the buttons to change the box!</p>
<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>
<button id="button1">Grow</button>
<button id="button2">Blue</button>
<button id="button3">Fade</button>
<button id="button4">Reset</button>
<script type="text/javascript" src="Javascript.js"></script>
</body>
</html>
更改 HTML 元素的背景颜色而不是颜色 属性。
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.backgroundColor = "blue";
});
请在下面找到工作代码片段:
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.backgroundColor = "blue";
});
<!DOCTYPE html>
<html>
<head>
<title>Jiggle Into JavaScript</title>
</head>
<body>
<p>Press the buttons to change the box!</p>
<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>
<button id="button1">Grow</button>
<button id="button2">Blue</button>
<button id="button3">Fade</button>
<button id="button4">Reset</button>
</body>
</html>
您使用错误的属性将颜色更改为背景
<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>
<button id="button0">Grow</button>
<button onclick="Grow()" id="button1">Grow</button>
<button onclick="Blue()" id="button2">Blue</button>
<button onclick="Fade()" id="button3">Fade</button>
<button onclick="Reset()" id="button4">Reset</button>
const box = document.getElementById('box');
document.getElementById('button0').addEventListener('click', e => {
box.style.width = '500px';
box.style.height = '500px';
})
const Grow = e => {
box.style.width = '300px';
box.style.height = '300px';
}
const Blue = e => {
box.style.background = 'blue';
}
const Reset = e => {
box.setAttribute('style', 'height:150px; width:150px; background-color:orange; margin:25px');
}
我正在尝试在单击按钮 2(名为“蓝色”)时将框的颜色更改为蓝色。
这是无效的代码....有人能帮忙吗?
演示
document.getElementById("button1").addEventListener("click", function(){
document.getElementById("box").style.height = "300px";
document.getElementById("box").style.width = "300px";
});
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.color = 'blue';
});
<!DOCTYPE html>
<html>
<head>
<title>Jiggle Into JavaScript</title>
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> -->
</head>
<body>
<p>Press the buttons to change the box!</p>
<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>
<button id="button1">Grow</button>
<button id="button2">Blue</button>
<button id="button3">Fade</button>
<button id="button4">Reset</button>
<script type="text/javascript" src="Javascript.js"></script>
</body>
</html>
更改 HTML 元素的背景颜色而不是颜色 属性。
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.backgroundColor = "blue";
});
请在下面找到工作代码片段:
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.backgroundColor = "blue";
});
<!DOCTYPE html>
<html>
<head>
<title>Jiggle Into JavaScript</title>
</head>
<body>
<p>Press the buttons to change the box!</p>
<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>
<button id="button1">Grow</button>
<button id="button2">Blue</button>
<button id="button3">Fade</button>
<button id="button4">Reset</button>
</body>
</html>
您使用错误的属性将颜色更改为背景
<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>
<button id="button0">Grow</button>
<button onclick="Grow()" id="button1">Grow</button>
<button onclick="Blue()" id="button2">Blue</button>
<button onclick="Fade()" id="button3">Fade</button>
<button onclick="Reset()" id="button4">Reset</button>
const box = document.getElementById('box');
document.getElementById('button0').addEventListener('click', e => {
box.style.width = '500px';
box.style.height = '500px';
})
const Grow = e => {
box.style.width = '300px';
box.style.height = '300px';
}
const Blue = e => {
box.style.background = 'blue';
}
const Reset = e => {
box.setAttribute('style', 'height:150px; width:150px; background-color:orange; margin:25px');
}