当前位置:网站首页>Interlaced color change of table
Interlaced color change of table
2022-07-22 14:11:00 【ca11meback】
First, we get such a table
Our goal is to randomly point a grid , Then this column of grids changes color , When you click on another grid, this column of grids changes back to the original color , Then change the color of the column of cells you click
To achieve this goal , Two things we need to learn
The first is the function we introduced in the previous article , If you don't understand, you can read the previous article .
It is used to find the number of child elements of the parent element
Object.prototype.indexof1=function(){
var arr=this.parentElement.children
for(let i=0;i<arr.length;i++){
if(arr[i]===this){
return i
}
}
}
The second point is , Exclusive thoughts :
About exclusivity , My understanding is to exclude everyone first , Finally, add the desired effect to yourself .
var tb=document.querySelector("#tb")
var trs=document.querySelectorAll("tr")
var tds=document.querySelectorAll("td")
for(let i=0;i<tds.length;i++){
tds[i].onclick=function(){
// console.log(this,tds[i])
tds.forEach((el)=>{
// console.log(el,11)
el.style.backgroundColor="white"
})
let x=tds[i].indexof1()
trs.forEach((el)=>{
//el yes tr Elements
el.children[x].style.backgroundColor="red"
})
}
}
First, we get tr and td label
Use for Give all the loops td Are tied to click events
Click in the event function
1. adopt tds( The tag selector filters out an array of classes ) Use the loop to put all td Change the background color of the label to white
2. Through the function we designed, the cell returned by clicking is the number of child elements of the parent element , return i come out
3. Through this i Go through each one tr The number in the label i individual td Elements change the background color .
This whole idea is exclusive .
Static will not be described
Let's talk about one more How does each line appear
var trs=document.querySelectorAll("#tb3 tr")
for(let i=0;i<trs.length;i++){
trs[i].onclick=function(){
trs.forEach((el)=>{
el.style.backgroundColor="white"
})
// console.log(i,trs[i],11111)
trs[i].style.backgroundColor="gray"
}
}
})()
No need if you can td The click event is bound in
Directly in tr Binding events in .
You don't need to get it through the set function i The subscript .
Direct manipulation tr Just a label
边栏推荐
- 微服务高可用的两个关键技巧,你一定用得上
- 实习打怪之路:JS中检测数据类型的方法
- 8. ZABBIX distributed
- 7. ZABBIX auto discovery automatic logon
- Yolov7 experiment test II: remote sensing image detection application (yolov7 tiny silu.yaml)
- NVIDIA programmable inference accelerator tensorrt learning notes (I) -- start
- Under file operation (C language)
- 2.zabbix概念
- [take you to the cloud native system] Part 4: kubernetes from entry to mastery
- Pytoch (III) -- fashionmnist fashion classification
猜你喜欢
After reading this, I can't DVMA yet. Please eat melon
JUC concurrent contracting
Hot pot restaurant ordering system based on ssm+jsp+mysql+h-ui
Go语言 Go命令行工具
Add Chinese and other fonts to R plot [showtext]
Yolov7 experiment test II: remote sensing image detection application (yolov7 tiny silu.yaml)
Pytoch (III) -- fashionmnist fashion classification
美团二面: Redis 5 种基础数据结构?
AQS of concurrent memory model -- reentrantlock locking process
7. ZABBIX auto discovery automatic logon
随机推荐
Super dry! Thoroughly understand the differences and connections between simplex, half duplex and full duplex
来get一个超实用的小页面——todolist
mysql
Load and display the label information of raincityspaces
Use of split function in MATLAB
Wheel 5: qcustomplot common classes
9. ZABBIX SNMP monitoring
第 3 章 处理数据
Cohérence finale transactions distribuées STC
ctf (套娃)
5.zabbix创建自定义key
C language: leak detection and filling (II)
Address book (file version)
OpenCV 常用的API总结(迅速查询)
实习打怪之路:JS中检测数据类型的方法
4.部署agent2
Microservice practice | centralized configuration center config symmetric encryption practice
正则 删除特定字符后面所有的元素(不区分大小写)
I met a 38K from Tencent two days ago, which showed me the ceiling of the foundation. Today I give it to you~
Cache and redirection in HTTP practice