[英]How to change background colour of cell
基本上,我正在创建一个4x4的灯板,我希望按顺序点亮某些单元格,我希望首先更改背景色的单元格的ID是yellow1,是否像这样,因为它不会更改atm的颜色
注意:页面加载时为灰色( #404040
)
抱歉,这么早的短代码让我在移动中思考。 这是我带有表的完整.html文件。 基本上我希望黄色1变黄,然后当那是黄色时我希望blue2变蓝并且黄色变回灰色,然后我希望green3变绿色,而蓝色变回灰色,依此类推直到orange5然后我想变回yellow1并使其无限循环直到文档关闭。 这是我的全部代码,我仍然无法获得任何要更改颜色的单元格。 我还希望它在颜色变化之间等待1.5秒,因为您可以看到ive进入了setInterval函数,但仍然没有运气。
<head>
<title>Light Board</title>
</head>
<body>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
</style>
<table id="lightboard" class="tg" style=undefined;table-layout: fixed; width: 526px">
<colgroup>
<col style="width: 124px">
<col style="width: 129px">
<col style="width: 133px">
<col style="width: 140px">
</colgroup>
<tr>
<th class="tg-3as3" style="background-color:#404040";></th>
<th class="tg-3as3" style="background-color:#404040";></th>
<th class="tg-3as3" style="background-color:#404040";></th>
<th id="white4" class="tg-3as3" style="background-color:#404040";></th>
</tr>
<tr>
<td class="tg-3as3" style="background-color:#404040";></td>
<td id="yellow1" class="tg-3as3" style="background-color:#404040";></td>
<td class="tg-3as3" style="background-color:#404040";></td>
<td class="tg-3as3" style="background-color:#404040";></td>
</tr>
<tr>
<td id="blue3" class="tg-3as3" style="background-color:#404040";></td>
<td class="tg-3as3" style="background-color:#404040";></td>
<td id="green2"class="tg-3as3" style="background-color:#404040";></td>
<td class="tg-3as3" style="background-color:#404040";></td>
</tr>
<tr>
<td class="tg-3as3" style="background-color:#404040";></td>
<td id="orange5" class="tg-3as3" style="background-color:#404040";></td>
<td class="tg-3as3" style="background-color:#404040";></td>
<td class="tg-3as3" style="background-color:#404040";></td>
</tr>
</table>
</body>
<script>
var yellow1 = document.getElementById("yellow1");
var green2 = document.getElementById("green2");
var blue3 = document.getElementById("blue3");
var white4 = document.getElementById("white4");
var orange5 = document.getElementById("orange5");
window.addEventListener("load", function() {
yellow1.style.backgroundColor = "#404040";
})
tInterval()
function tInterval() {
tInterval = setInterval(doSequence, 1500)
}
function doSequence() {
if (yellow1.style.backgroundColor == "#404040")
{
yellow1.style.backgroundColor="rgb(255,255,0)";
return;
}
if(yellow1.style.backgroundColor == "rgb(255,255,0)")
{
yellow1.style.backgroundColor == "#404040";
green2.style.backgroundColor="rgb(0,255,0)";
return;
}
}
</script
还要检查getElementById,您的ID大写应该为“ ... Id”,
抱歉,我还不能添加评论,因为我没有足够的分数,所以不得不将其写为答案
我可以看到您的getElementByID语法错误,应该是这样的:
var yellow1 = document.getElementById("yellow1");
并为该元素设置颜色,例如:
yellow1.style.backgroundColor = "#FF0000";
谢谢
javascript无法访问css的值,您应该在if语句之前使用javascript分配其颜色
var yellow1 = document.getElementById("yellow1");
window.addEventListener('load', function(){
yellow1.style.backgroundColor = "#404040";
})
if(yellow1.style.backgroundColor == "rgb(64, 64, 64)")
{ yellow1.style.backgroundColor = "#FFFF00"; }
现在可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.