当前位置:网站首页>原生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()
}
}
边栏推荐
猜你喜欢
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
浏览器打印边距,默认/无边距,占满1页A4
< li> dot style list style type
Advancedinstaller installation package custom action open file
力扣——第298场周赛
1605. Sum the feasible matrix for a given row and column
Ball Dropping
QT implementation window gradually disappears qpropertyanimation+ progress bar
分享一个在树莓派运行dash应用的实例。
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
随机推荐
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
1605. Sum the feasible matrix for a given row and column
Determine the Photo Position
C language learning notes
Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)
C language must memorize code Encyclopedia
日期加1天
useEffect,函数组件挂载和卸载时触发
Configuration du cadre flask loguru log Library
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
HDU - 6024 building shops (girls' competition)
Codeforces Round #802(Div. 2)A~D
Codeforces Round #800 (Div. 2)AC
MariaDB的安装与配置
Some problems encountered in installing pytorch in windows11 CONDA
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
Pyside6 signal, slot
Installation and configuration of MariaDB
Frida hook so layer, protobuf data analysis
去掉input聚焦时的边框