本帖最后由 iceboo 于 2012-07-10 11:48:48 编辑

解决方案 »

  1.   

    在window.onload的时候就判断checkbox的状态,如果为checked为true,就给后面的label加上背景色。点击就是onclick的时候判断,为true就加上,false就去掉。
      

  2.   

    <!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>
    &nbsp;<br>
    <input type=checkbox name=ztid[] id='check3' value='3'>
    <label for='check3'>作文</label>
    &nbsp;<br>
    <input type=checkbox name=ztid[] id='check30' value='30'>
    <label for='check30'>零分作文</label>
    &nbsp;<br>
    <input type=checkbox name=ztid[] id='check31' value='31' checked>
    <label for='check31'>满分作文</label>
    </body>
    </html>
      

  3.   

    为啥要点击文字再用checkbox呢
      

  4.   


    if (document.getElementById(oLabels[i].getAttribute('for')).checked) oLabels[i].style.backgroundColor = '#FF0'; //初始化label背景色
    这一句好像有点问题。
      

  5.   


    因为功能开始就要checkbox  但是点击区域太小,就设计成点文字也能选择checkbox后来再想把文字也变色,让选择更加明确。主要是为了操作的人更方便