当前位置:网站首页>Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog

Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog

2022-07-06 16:39:00 Feng Hao (grow up)

Ideas : First, we get the nodes

		 Traverse each node   Get checked Properties of 
		 Then we change checked The attribute of changes the selected state 
		 We let them all vote for true 
		 We use ! To achieve results 

 Insert picture description here

The knowledge points used this time are :

document.getElementsByName(‘name attribute ’): Check all name by name Attribute's element node object 
document.getElementById('id name '):  adopt id Name get an element node object 
onclick: Mouse click event 

html part :

<input id = 'quan' type="checkbox"  value = ' Future generations '>  Future generations 
<input type="checkbox" name="items" value=" football " > football 
<input type="checkbox" name="items" value=" Basketball " > Basketball 
<input type="checkbox" name="items" value=" badminton " > badminton 
<input id= "fan" type="checkbox"  value=" Reverse election "> Reverse election 

js part

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()
    }
}

 Insert picture description here

原网站

版权声明
本文为[Feng Hao (grow up)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060920252234.html