javascript全选框案例

Original 2019-02-14 20:00:52 261
abstract:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">&

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

<script src="demo/js/jq_3.3.1_mi.js"></script>

</head>

<body>

<h1>欢迎来到javascript的世界</h1>

<div><span class="sl"></span></div>

<div><span class="sl"></span></div>

<div><span class="sl"></span></div>

<!-- <input type="text" onfocus='aaa(this)' onblur="bbb(this)"   onkeyup="bbb(this)" onsubmit="bbb(this)">

   <button onsubmit="bbb(this)" onmouseout="bbb(this)">aaa</button> -->

<!-- <div style="width:50px;height:50px;border:1px solid #ff6500;" onmousemove="aaa(this)" onmouseout="bbb(this)"><p id="a1">123</p></div> -->

<!-- <img src="demo\0129\static\images\8.jpg" id="a1" onclick="aaa(this)"> -->

<script>

// function aaa(x){

//         x.style.borderRadius='24px';

//         document.getElementById('a1').innerHTML='你有病';

//         document.getElementById('a1').src='demo\0129\static\images\8.jpg';

//     }

// function bbb(x){

//     x.style.borderRadius='';

//     }

// function ccc(x){

//         x.style.background="red"

//     }

// function aaa(x){

//         document.getElementById('a1').src='https://img.php.cn/upload/avatar/000/320/998/5c43882c1633e789.jpg';

//     }

// 输出月份

var a=new Date()

var b=new Date()

var month=new Array(12)

month[0]='1'

month[1]='2'

month[2]='3'

month[3]='4'

month[4]='5'

month[5]='6'

month[6]='7'

month[7]='8'

month[8]='9'

month[9]='10'

month[10]='11'

month[11]='12'

console.log(month[a.getMonth()])

</script>

<script>

// console.log($('div').children('.sl').length)

// console.log($('div').hasClass('.sl').length)

// if($('div').hasClass('.sl')){

//     alert('true')

// }else{

//     alert('flase')

// }

// var js='hello javascript';

// for(var js=1;js<99;js++){console.log(js)}

// var y=8;

// do{

//     document.write(y+'<br>');

//     y++;

// }while(y<=11);

// var a=3,b='hello word';

// switch(a){

//     case 1:document.write('等于1')

//     break;

//     case 2:document.write('等于2')

//     break;

//     default:document.write('以上值都不匹配');

// }

// document.write('<br>');

// document.write(b.charAt(0));

// document.write('<br>');

// document.write(b.indexOf("llo"));

// document.write('<br>');

// document.write(b.lastIndexOf("o"));

// document.write('<br>');

// document.write(b.substring(0,4));

// document.write('<br>');

// document.write(b.replace('o','php中文网'));

// document.write('<br>');

// var a='你好',b='123徐家淇1234',c=2019,text='123456789';

// var d='123.123徐家淇1234';

// document.write(a.concat(b,c));

// document.write('<br>');

// var str="Hello world!"

// document.write(text.substr(3));

// document.write('<br>');

// document.write(parseInt(d));

// document.write('<br>');

// document.write(parseFloat(d));

// document.write('<br>');

// // var cc=c+'';

// // document.write(typeof(cc));

// // document.write('<br>');

// // var aa=String(c);

// // document.write(typeof(aa));

// document.write('<br>');

// var bb=c.toString();

// document.write(typeof(bb));

var x,b;

// var a=new Array(2)

var a=Array(1,2,100,1000,212,4546,88,22,33,3,4)

// a[0] = new Array(0,1,2)

// a[1] = new Array(3,4,5)

// console.log(a[1][2])

// a[1][2]=100;

// console.log(a[1][2])

for(x in a){

document.write(a[x]+'<br>');

}

// b=a[0].concat(a[1])

// b=a.join('_')

console.log(a.reverse());

console.log(a.sort());

a.push('合肥','徐家淇');

a.pop();

var x='情人眼中出狗屎',y='什么名字';

// alert(a);

// confirm(x);

// prompt(y);

// function txt(name,age){

//     alert('姓名为:'+name+',年龄为:'+age)

// }

// txt('徐家淇','28');

</script>

<div id="box" style='height: 50px;width: 50px;background:pink;margin: 20px 100px'></div>

<input type="button" value="变高" onclick="aa()">

<input type="button" value="变宽" onclick="bb()">

<input type="button" value="变色" onclick="cc()">

<input type="button" value="重置" onclick="dd()">

<input type="button" value="隐藏" onclick="ee()">

<input type="button" value="显示">

<script>

var box

window.onload=function(){

box=document.getElementById('box')

}

function aa(){

box.style.height='100px';

}

function bb(){

box.style.width='100px';

}

function cc(){

box.style.background='#ff6500';

}

function dd(){

box.style.height='50px';

box.style.width='50px';

box.style.background='pink';

box.style.display='block';

}

function ee(){

box.style.display='none';

}

</script>

<div class="box1">

<div>

<input type="checkbox" id="checkall" onclick="checkAll()"><label for="checkall">全选</label>

</div>

<input type="checkbox" name='item[]'><label for="checkall">1</label>

<input type="checkbox" name='item[]'><label for="checkall">2</label>

<input type="checkbox" name='item[]'><label for="checkall">3</label>

<input type="checkbox" name='item[]'><label for="checkall">4</label>

<input type="checkbox" name='item[]'><label for="checkall">5</label>

<input type="checkbox" name='item[]'><label for="checkall">6</label>

</div>

<script>

function checkAll(){

var checkall,item;

checkall=document.getElementById('checkall');

item=document.getElementsByName('item[]');

for(var i=0;i<item.length;i++){

if(checkall.checked){

item[i].checked=true;

}else{

item[i].checked=false;

}

}

}

</script>

总结:

1、创建两个变量checkall,item,checkall用来存储查找id为checkall,item存储name名为item的复选框

2、创建for循环遍历名字为item的值,如果选中id为checkall的全选框,则item【i】的选框全部为true,否则全部不选


Correcting teacher:韦小宝Correction time:2019-02-15 09:14:12
Teacher's summary:写的很不错 JavaScript全选还是比较作用的 不管是在后台管理中或者是在前台页面基本上都是会用到的 一定要好好掌握!

Release Notes

Popular Entries