js实现:
DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><meta http-equiv&#61;"X-UA-Compatible" content&#61;"IE&#61;edge"><meta name&#61;"viewport" content&#61;"width&#61;device-width, initial-scale&#61;1.0"><title>Documenttitle>
head>
<body><script type&#61;"text/Javascript">window.onload &#61; function(){var quan &#61; document.getElementById("quan");quan.onclick &#61; function(){var aihaos &#61; document.getElementsByName("aihao");for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){if(quan.checked){aihaos[i].checked &#61; true;}else{aihaos[i].checked &#61; false;}}}}script><input type&#61;"checkbox" id&#61;"quan"/><br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"smoke"/>抽烟<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"drink"/>喝酒<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"firehair"/>烫头<br>
body>
html>
简化版&#xff1a;
DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><meta http-equiv&#61;"X-UA-Compatible" content&#61;"IE&#61;edge"><meta name&#61;"viewport" content&#61;"width&#61;device-width, initial-scale&#61;1.0"><title>Documenttitle>
head>
<body><script type&#61;"text/Javascript">window.onload &#61; function(){var quan &#61; document.getElementById("quan");quan.onclick &#61; function(){var aihaos &#61; document.getElementsByName("aihao");for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){aihaos[i].checked &#61; quan.checked;}}}script><input type&#61;"checkbox" id&#61;"quan"/><br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"smoke"/>抽烟<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"drink"/>喝酒<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"firehair"/>烫头<br>
body>
html>
上面两种都有错误&#xff0c;全部选中后&#xff0c;取消下面任意一个&#xff0c;上面的全选还会打钩&#xff0c;下面是最终的完善版本&#xff1a;
DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><meta http-equiv&#61;"X-UA-Compatible" content&#61;"IE&#61;edge"><meta name&#61;"viewport" content&#61;"width&#61;device-width, initial-scale&#61;1.0"><title>Documenttitle>
head>
<body><script type&#61;"text/Javascript">window.onload &#61; function(){var quan &#61; document.getElementById("quan");var aihaos &#61; document.getElementsByName("aihao");quan.onclick &#61; function(){for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){aihaos[i].checked &#61; quan.checked;}}for(var i &#61; 0; i<aihaos.length ; i&#43;&#43;){aihaos[i].onclick &#61; function(){var count &#61; aihaos.length;var checkedCount &#61; 0;for(var i &#61; 0; i<aihaos.length; i&#43;&#43;){if(aihaos[i].checked){checkedCount&#43;&#43;;}}quan.checked &#61; (count &#61;&#61;checkedCount);}}}script><input type&#61;"checkbox" id&#61;"quan"/><br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"smoke"/>抽烟<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"drink"/>喝酒<br><input type&#61;"checkbox" name&#61;"aihao" value&#61;"firehair"/>烫头<br>
body>
html>