我无法setAttribute()
甚至无法createAttribute()
将 className 添加到新创建的div
元素中。值得注意的是,div 是使用命令在fetch().then()
链内创建的。createElement
代码看起来像这样......
document.addEventListener("DOMContentLoaded", () => {
let row = 0
fetch(domain + "/boards/new")
.then(resp => resp.json())
.then(json => json.spaces)
.then(spaces => spaces.forEach( (iRow) => {
let col = 0
iRow.forEach( (iCol) => {
if (iCol != null){
let checker = document.createElement('div')
let color = iCol.team
let c_id = iCol.id
checker.createAttribute(className)
checker.setAttribute(className, `${color}-checker`)
checker.createAttribute(id)
checker.setAttribute(id, c_id)
checker.createAttribute(data-pos)
checker.setAttribute(data-pos, `[${row}][${col}]`)
document.body.appendChild(checker)
}
col = col + 1
})
row = row + 1
}))
})
它一直到该行checker.createAttribute(className)
但随后出错,说'className is note defined'这很奇怪,编译器应该将className理解为全局HTML属性,不是吗?