当前位置:网站首页>原生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()
}
}
边栏推荐
- 本地可视化工具连接阿里云centOS服务器的redis
- Vs2019 initial use
- 指定格式时间,月份天数前补零
- Codeforces Round #798 (Div. 2)A~D
- Opencv learning log 26 -- detect circular holes and mark them
- Flask框架配置loguru日志库
- 2027. Minimum number of operations to convert strings
- 去掉input聚焦时的边框
- Codeforces Round #800 (Div. 2)AC
- (POJ - 3258) River hopper (two points)
猜你喜欢
1013. Divide the array into three parts equal to and
树莓派4B安装opencv3.4.0
Kubernetes集群部署
Basic Q & A of introductory C language
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Problem - 922D、Robot Vacuum Cleaner - Codeforces
B - Code Party (girls' competition)
去掉input聚焦时的边框
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
Codeforces Round #802(Div. 2)A~D
随机推荐
QT implementation window gradually disappears qpropertyanimation+ progress bar
生成随机密码/验证码
QT实现窗口渐变消失QPropertyAnimation+进度条
Openwrt build Hello ipk
300th weekly match - leetcode
Is the sanic asynchronous framework really so strong? Find truth in practice
Summary of FTP function implemented by qnetworkaccessmanager
875. Leetcode, a banana lover
Codeforces Round #798 (Div. 2)A~D
7-1 understand everything (20 points)
QT实现圆角窗口
MariaDB的安装与配置
Codeforces Round #799 (Div. 4)A~H
拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
读取和保存zarr文件
Bidirectional linked list - all operations
Analysis of protobuf format of real-time barrage and historical barrage at station B
Date plus 1 day
Acwing: Game 58 of the week
F - birthday cake (Shandong race)