js初学—实现checkbox全选功能

布局如下:

<p ><input type="checkbox" id="che1"/>全选</p>
<
div id="div1"> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> <input type="checkbox" /><br /> </div>

javascript如下:

<script type="text/javascript">
    window.onload=function()
    {
        xuanzhe();
    }
    function xuanzhe()//定义函数
    {
        var oChe1=document.getElementById('che1');//获取che1
        var oDiv1=document.getElementById('div1').getElementsByTagName('input');//获取div里面的所有input

                oChe1.onclick=function()//定义che1的鼠标点击事件
                {
                    
                        for(var i=0;i<oDiv1.length;i++)
                        {
                            if(che1.checked==true)
                            {
                                oDiv1[i].checked=true;
                            }
                            else
                            {
                                oDiv1[i].checked=false;
                            }
                        }
                
                    
                };
    }

</script>

能实现checkbox全选功能!  这种功能在网页是应用很广泛!

posted @ 2014-08-31 20:35  GangZhan  阅读(211)  评论(0编辑  收藏  举报