js代码
var tb = document.getElementById("tb")
var tr = document.querySelectorAll("tr") //获取tr元素
var td = document.querySelectorAll("td") //获取td元素
for (i = 0; i < tr.length; i++) {
console.log(tr[i].children.length);
for (x = 0; x < tr[i].children.length; x += 2) {
tr[i].children[x].style.backgroundColor = 'blue';
}
}
//设计一个函数,得到每一个td在tr中的位置
Object.prototype.index = function () {
var arrSon = this.parentElement.children
for (let i = 0; i < arrSon.length; i++)
if (this == arrSon[i]) {
return i
}
}
for (let i = 0; i < td.length; i++) { //遍历每一个td元素
td[i].onclick = function () { //给每一个td元素绑定 点击事件
td.forEach(element => {
element.style.backgroundColor = "white" //让所有td变为白色
});
let b = td[i].index() //调用函数,得到当前td在tr中的位置
tr.forEach(el => {
el.children[b].style.backgroundColor = "blue"
//让所有tr中当前td的位置全部变为蓝色,也就是当前td所在的一竖列变蓝
})
}
}