【发布时间】:2015-03-24 19:55:43
【问题描述】:
我在document.getElementById(blueChild).style.border = "2px solid white" 线上收到错误Uncaught TypeError: Cannot read property 'style' of null,我在网上查看并没有找到解决方案。这是我的代码:
var scroller = function(direction, team){
console.log("scrolling")
if (team == "blue"){
if (direction == "left"){
blueCount = blueCount - 1
}
if (blueCount < 0){
blueCount = 3
}
if (redCount < 0){
redCount = 3
}
var blueChildren = document.getElementById('blueSelector').getElementsByTagName('div')
var blueChild = blueChildren[blueCount]
document.getElementById(blueChild).style.border = "2px solid white"
for (var i = 0; i < blueChildren.length; i++){
if (i !== blueCount){
blueChild = blueChildren[i]
document.getElementById(blueChild).style.border = "2px solid black"
}
}
}
}
scroller("left", "blue")
顺便说一下,blueCount、redCount 的全局默认值为 0
大量请求的 HTML
<html>
<head>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="main.css">
<script src="jquery.js"></script>
<script src="rectangle.js"></script>
<script src="main.js"></script>
</head>
<body>
<canvas id="canvas">You're browser does not support the canvas tag. Go and get a real browser, nub</canvas>
<div id="redSelector">
<div class="genericButton" id="zombieRed">Zombie</div>
<div class="genericButton" id="skeletonRed">Skeleton</div>
<div class="genericButton" id="creeperRed">Creeper</div>
<div class="genericButton" id="spideBlue">Spider</div>
</div>
<div id="blueSelector">
<div class="genericButton" id="zombieBlue">Zombie</div>
<div class="genericButton" id="skeletonBlue">Skeleton</div>
<div class="genericButton" id="creeperBlue">Creeper</div>
<div class="genericButton" id="spiderBlue">Spider</div>
</div>
</body>
我想要实现的是评估blueSelector 的子元素,并制作一个带有白色边框的颜色,并用黑色边框休息。
【问题讨论】:
-
考虑分号。如果不出意外,它会让你的代码更容易解释。
-
HTML 是什么样的?请发布一个完整的代码示例。
-
document.getElementById(blueChild)返回空值。否则,这条线应该可以工作。再次检查 ID 的拼写,单独执行命令document.getElementById(blueChild)验证是否返回 null。
标签: javascript html tags children