效果如下图
代码如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
<script type="text/javascript">
/**
* 全选
*/
function selectAll() {
var languages = document.getElementsByName("language");
for (var i=0; i < languages.length; i++) {
var language = languages[i];
language.checked = true;
}
}
/**
* 取消全选
*/
function deselectAll() {
var languages = document.getElementsByName("language");
for (var i=0; i < languages.length; i++) {
var language = languages[i];
language.checked = false;
}
}
/**
* 反选
*/
function inverse() {
var languages = document.getElementsByName("language");
for (var i=0; i < languages.length; i++) {
var language = languages[i];
if(language.checked) {
language.checked = false;
}else {
language.checked = true;
}
}
}
</script>
</head>
<body>
编程语言:<input type="checkbox" name="language" value="C" />C
<input type="checkbox" name="language" value="C++" />C++
<input type="checkbox" name="language" value="Java" />Java
<input type="checkbox" name="language" value="Python" />Python<br/>
<button οnclick="selectAll();">全选</button>
<button οnclick="deselectAll();">取消全选</button>
<button οnclick="inverse();">反选</button>
</body>
</html>