checkbox 点击勾选和变色问题 本帖最后由 iceboo 于 2012-07-10 11:48:48 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在window.onload的时候就判断checkbox的状态,如果为checked为true,就给后面的label加上背景色。点击就是onclick的时候判断,为true就加上,false就去掉。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript">window.onload = function() { var oLabels = document.getElementsByTagName('label'); for (var i = 0; i < oLabels.length; i ++) { if (document.getElementById(oLabels[i].getAttribute('for')).checked) oLabels[i].style.backgroundColor = '#FF0'; //初始化label背景色 oLabels[i].onclick = function() { var chkbox = document.getElementById(this.getAttribute('for')); if (chkbox.checked) this.style.backgroundColor = '#FFF'; else this.style.backgroundColor = '#FF0'; } } var o = document.getElementsByName('ztid[]'); for (var i = 0; i < o.length; i ++) { o[i].onclick = function() { var oNext = this.nextSibling; while (oNext.nodeType != 1) oNext = oNext.nextSibling; if (this.checked) oNext.style.backgroundColor = '#FF0'; else oNext.style.backgroundColor = '#FFF'; } }}</script></head><body><input type=checkbox name=ztid[] id='check2' value='2'><label for='check2'>高考</label> <br><input type=checkbox name=ztid[] id='check3' value='3'><label for='check3'>作文</label> <br><input type=checkbox name=ztid[] id='check30' value='30'><label for='check30'>零分作文</label> <br><input type=checkbox name=ztid[] id='check31' value='31' checked><label for='check31'>满分作文</label></body></html> 为啥要点击文字再用checkbox呢 if (document.getElementById(oLabels[i].getAttribute('for')).checked) oLabels[i].style.backgroundColor = '#FF0'; //初始化label背景色这一句好像有点问题。 因为功能开始就要checkbox 但是点击区域太小,就设计成点文字也能选择checkbox后来再想把文字也变色,让选择更加明确。主要是为了操作的人更方便 javascript图片压缩 想不通的一个问题~~ 赋值的textarea能分行显示吗? javascript 导出到excel的问题 求助:非IE浏览器JS获得选中文字在段落中的位置 js释放资源的问题,即使是《阿信》的树也存在这个问题 请问大家有没有做过甘特图?能不能给小弟一点提示。 滚动条怪问题! 如何是marquee的文字在初始的时候是不滚动的? 怎么获取select的值? 子窗口父窗口传值问题[支持Firefox] 超链接添加一个click事件,出现冒泡
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function() {
var oLabels = document.getElementsByTagName('label');
for (var i = 0; i < oLabels.length; i ++) {
if (document.getElementById(oLabels[i].getAttribute('for')).checked) oLabels[i].style.backgroundColor = '#FF0'; //初始化label背景色
oLabels[i].onclick = function() {
var chkbox = document.getElementById(this.getAttribute('for'));
if (chkbox.checked) this.style.backgroundColor = '#FFF';
else this.style.backgroundColor = '#FF0';
}
}
var o = document.getElementsByName('ztid[]');
for (var i = 0; i < o.length; i ++) {
o[i].onclick = function() {
var oNext = this.nextSibling;
while (oNext.nodeType != 1) oNext = oNext.nextSibling;
if (this.checked) oNext.style.backgroundColor = '#FF0';
else oNext.style.backgroundColor = '#FFF';
}
}
}
</script>
</head><body>
<input type=checkbox name=ztid[] id='check2' value='2'>
<label for='check2'>高考</label>
<br>
<input type=checkbox name=ztid[] id='check3' value='3'>
<label for='check3'>作文</label>
<br>
<input type=checkbox name=ztid[] id='check30' value='30'>
<label for='check30'>零分作文</label>
<br>
<input type=checkbox name=ztid[] id='check31' value='31' checked>
<label for='check31'>满分作文</label>
</body>
</html>
if (document.getElementById(oLabels[i].getAttribute('for')).checked) oLabels[i].style.backgroundColor = '#FF0'; //初始化label背景色
这一句好像有点问题。
因为功能开始就要checkbox 但是点击区域太小,就设计成点文字也能选择checkbox后来再想把文字也变色,让选择更加明确。主要是为了操作的人更方便