当前位置:网站首页>原生js实现全选和反选的功能 --冯浩的博客
原生js实现全选和反选的功能 --冯浩的博客
2022-07-06 09:29:00 【冯浩(grow up)】
思路:首先我们获取节点
遍历每个节点 获取到checked的属性
然后我们通过改变checked的属性改变选中的状态
全选的时候我们让他们都为true
反选的时候我们使用!达到效果
本次使用到的知识点有:
document.getElementsByName(‘name属性’):选中所有的name为name属性的的元素节点对象
document.getElementById('id名称'): 通过id名称获取一个元素节点对象
onclick:鼠标点击事件
html部分:
<input id = 'quan' type="checkbox" value = '全选'> 全选
<input type="checkbox" name="items" value="足球" >足球
<input type="checkbox" name="items" value="篮球" >篮球
<input type="checkbox" name="items" value="羽毛球" >羽毛球
<input id= "fan" type="checkbox" value="反选">反选
js部分
var items = document.getElementsByName('items');
var quan = document.getElementById('quan');
var fan = document.getElementById('fan');
function fan1(){
var index = 0;
for(var i =0;i<items.length;i++){
items[i].checked = !items[i].checked;
if(items[i].checked==true){
index ++;
}
}
if(index == 3){
quan.checked = true;
}else{
quan.checked =false;
}
}
quan.onclick=function(){
if(quan.checked ==true){
console.log(items[0].checked);
for(var i = 0;i < items.length;i++){
console.log(items[i].checked);
items[i].checked = true;
fan.checked=false;
}
} else{
for(var i = 0;i < items.length;i++){
// console.log(items[i].checked);
items[i].checked = !true;
fan.checked=false;
}
}
};
fan.onclick =function(){
if(fan.checked== true){
fan1()
}else{
fan1()
}
}
边栏推荐
- The "sneaky" new asteroid will pass the earth safely this week: how to watch it
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- Opencv learning log 28 -- detect the red cup cover
- 力扣:第81场双周赛
- D - function (HDU - 6546) girls' competition
- 875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
- Li Kou: the 81st biweekly match
- 图图的学习笔记-进程
- Codeforces Round #799 (Div. 4)A~H
- Useeffect, triggered when function components are mounted and unloaded
猜你喜欢
Suffix expression (greed + thinking)
1323. Maximum number of 6 and 9
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
Some problems encountered in installing pytorch in windows11 CONDA
7-1 understand everything (20 points)
Codeforces Round #799 (Div. 4)A~H
QT实现窗口渐变消失QPropertyAnimation+进度条
It is forbidden to trigger onchange in antd upload beforeupload
QT模拟鼠标事件,实现点击双击移动拖拽等
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
随机推荐
AcWing:第58场周赛
Hdu-6025-prime sequence (girls' competition)
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
It is forbidden to trigger onchange in antd upload beforeupload
Openwrt build Hello ipk
QT realizes window topping, topping state switching, and multi window topping priority relationship
1323. Maximum number of 6 and 9
去掉input聚焦时的边框
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
Oneforall installation and use
605. Planting flowers
Penetration test (4) -- detailed explanation of meterpreter command
Summary of FTP function implemented by qnetworkaccessmanager
Frida hook so layer, protobuf data analysis
Codeforces - 1526C1&&C2 - Potions
Classic application of stack -- bracket matching problem
QT实现圆角窗口
Acwing - game 55 of the week
Codeforces Round #798 (Div. 2)A~D
力扣——第298场周赛