当前位置:网站首页>原生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()
}
}

边栏推荐
- Sanic异步框架真的这么强吗?实践中找真理
- QT实现圆角窗口
- Vs2019 initial use
- 7-1 understand everything (20 points)
- Calculate the time difference
- Kubernetes集群部署
- 1605. Sum the feasible matrix for a given row and column
- It is forbidden to trigger onchange in antd upload beforeupload
- Bidirectional linked list - all operations
- Is the sanic asynchronous framework really so strong? Find truth in practice
猜你喜欢

Borg maze (bfs+ minimum spanning tree) (problem solving report)

1005. Maximized array sum after K negations

OneForAll安装使用

It is forbidden to trigger onchange in antd upload beforeupload

Sword finger offer II 019 Delete at most one character to get a palindrome

Pyside6 signal, slot

Codeforces Round #802(Div. 2)A~D

Determine the Photo Position

QWidget代码设置样式表探讨

Openwrt source code generation image
随机推荐
The concept of C language array
(POJ - 3258) River hopper (two points)
window11 conda安装pytorch过程中遇到的一些问题
409. Longest palindrome
Study notes of Tutu - process
生成随机密码/验证码
807. Maintain the urban skyline
Specify the format time, and fill in zero before the month and days
HDU - 6024 building shops (girls' competition)
Codeforces - 1526C1&&C2 - Potions
Suffix expression (greed + thinking)
Interesting drink
Codeforces Round #802(Div. 2)A~D
useEffect,函數組件掛載和卸載時觸發
C language learning notes
QT实现窗口置顶、置顶状态切换、多窗口置顶优先关系
Codeforces Round #800 (Div. 2)AC
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
Codeforces Round #798 (Div. 2)A~D