Javascript 需要一些指导吗
只是想知道是否有人可以看看这段代码,让我知道我是否做对了。尝试循环遍历allCells对象集合,并将背景颜色设置为白色,然后运行事件处理程序,在单击每个单元格时运行changeColor()函数。谢谢Javascript 需要一些指导吗,javascript,object,styles,Javascript,Object,Styles,只是想知道是否有人可以看看这段代码,让我知道我是否做对了。尝试循环遍历allCells对象集合,并将背景颜色设置为白色,然后运行事件处理程序,在单击每个单元格时运行changeColor()函数。谢谢 window.onload = setPuzzle; var allCells; function setPuzzle() { var puzzleTable = document.getElementById("puzzleCells"); var allCells = do
window.onload = setPuzzle;
var allCells;
function setPuzzle() {
var puzzleTable = document.getElementById("puzzleCells");
var allCells = document.getElementsByName("puzzleTable");
for (var i = 0; i < allCells.length; i++) {
allCells[i].style.backgroundcolor = "white";
}
for (var i = 0; i < allCells.length; i++) {
allCells[i].onclick = changeColor()
}
document.getElementById("solution").onclick = showSolution();
document.getElementById("hide").onclick = hideSolution();
document.getElementById("check").onclick = checkSolution();
document.getElementById("uncheck").onclick = uncheckSolution();
}
window.onload=setPuzzle;
所有细胞;
函数setPuzzle(){
var puzztable=document.getElementById(“puzzCells”);
var allCells=document.getElementsByName(“拼图表”);
对于(var i=0;i
在第一次循环之前
allCells[i].document.getElementsByName(allCells)
应该是这样的
allCells = document.getElementsByName( [[ NAME ]] );
及
无法工作,因为allCells未设置为任何值
将allCells设置为元素列表后,可以按如下方式对其进行迭代:
allCells[i].style.backgroundColor = "white";
JavaScript是区分大小写的,所以您必须编写backgroundColor而不是backgroundColor
更新:要将函数分配给事件,必须编写
.onclick = yourFunction;
不是
从长远来看,更好的做法是:
.addEventListener("click", yourFunction);
第一个for循环看起来很奇怪,您应该有allCells[i]。样式。。。我猜当你尝试它时会发生什么?我还没试过运行它。这是课程的开始,我想确认一下我的回答是否正确。我编辑了上面的代码以包含allCells[I]代码。如果我试图将所有单元格bg颜色更改为白色,是否需要document.getElementsByTagName(所有单元格)?
.onclick = yourFunction();
.addEventListener("click", yourFunction);