Javascript 尝试更改子div属性时出错
我收到错误Javascript 尝试更改子div属性时出错,javascript,html,tags,children,Javascript,Html,Tags,Children,我收到错误未捕获类型错误:无法读取document.getElementById(blueChild).style.border=“2px solid white”行上null的属性“style”,我已联机查看,尚未找到解决方案。这是我的密码: var scroller = function(direction, team){ console.log("scrolling") if (team == "blue"){ if (direction == "left"
未捕获类型错误:无法读取document.getElementById(blueChild).style.border=“2px solid white”
行上null的属性“style”
,我已联机查看,尚未找到解决方案。这是我的密码:
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")
我试图实现的是评估
blueSelector的子元素,并使一个元素用白色边框着色,其余元素用黑色边框。您只需执行以下操作:
blueChild.style.border = "2px solid white"
及
因为您创建的blueChild
变量指向HTML元素,而不是表示ID的字符串
编辑:哦,你忘了引用一句话:
here
↓
scroller("left", "blue")
我找到了解决办法!事实证明,我使用var-blueCount
定义了blueCount
,当我在blueChild
数组中使用该变量作为索引时,blueCount
是未定义的
,而blueChild
则是未定义的
希望人们从我愚蠢的错误中学习xD考虑分号。如果没有其他东西,它会使代码更容易解释。HTML是什么样子的?请发布完整的代码示例。document.getElementById(blueChild)
返回空值。否则,这条线应该可以工作。再次检查ID的拼写,并分别执行命令document.getElementById(blueChild)
,以验证它是否返回null.Dosent work。我现在得到了错误未捕获类型错误:无法读取未定义的属性“style”
@JammehCarr我编辑了我的答案,你似乎忘记了引用我添加的HTML,你现在能知道你的问题是根据这个吗?@JammehCarr当你更改我说的内容时效果很好:但你需要加载文档。将您的
标签放在结束
标签的正上方。
blueChild.style.border = "2px solid black"
here
↓
scroller("left", "blue")