今日所学—JavaScript基础(二)

一、使用onload事件实现表格隔行换色

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>表格隔行换色</title>
        <script>
            window.onload = function() {
                //            获取表格
                var tblEle = document.getElementById("tbl");
                //           获取表格中tbody里的行数
                var len = tblEle.tBodies[0].rows.length;
                //           alert(len);
                //对tbody里的行进行遍历
                for(var i = 0; i < len; i++) {
                    if(i % 2 == 0) {
                        tblEle.tBodies[0].rows[i].style.backgroundColor = "pink";
                    } else {

                        tblEle.tBodies[0].rows[i].style.backgroundColor = "gold";

                    }
                }
            }
        </script>

    </head>

    <body>
        <table border="1" width="500" height="50" align="center" id="tbl">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>张三</td>
                    <td>22</td>
                </tr>
                <tr>
                    <td>2</td>
                    <td>李四</td>
                    <td>25</td>
                </tr>
                <tr>
                    <td>3</td>
                    <td>王五</td>
                    <td>27</td>
                </tr>
                <tr>
                    <td>4</td>
                    <td>赵六</td>
                    <td>29</td>
                </tr>
                <tr>
                    <td>5</td>
                    <td>田七</td>
                    <td>30</td>
                </tr>
                <tr>
                    <td>6</td>
                    <td>汾九</td>
                    <td>20</td>
                </tr>
            </tbody>
        </table>
    </body>

</html>

效果图:

 

 二、使用onmouseover和onmouseout实现鼠标移动到某一表格触发显示

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>鼠标触发显示</title>
        <script>
            function changeColor(id,flag){
                if(flag=="over"){
                    document.getElementById(id).style.backgroundColor="red";
                }else if(flag=="out"){
                    document.getElementById(id).style.backgroundColor="white";
                }
            }
        </script>

    </head>
    <body>
        <table border="1" width="500" height="50" align="center">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                </tr>
            </thead>
            <tbody>
                <tr onmouseover="changeColor('tr1','over')" id="tr1" onmouseout="changeColor('tr1','out')">
                    <td>1</td>
                    <td>张三</td>
                    <td>22</td>
                </tr>
                <tr onmouseover="changeColor('tr2','over')" id="tr2" onmouseout="changeColor('tr2','out')">
                    <td>2</td>
                    <td>李四</td>
                    <td>25</td>
                </tr>
                <tr onmouseover="changeColor('tr3','over')" id="tr3" onmouseout="changeColor('tr3','out')">
                    <td>3</td>
                    <td>王五</td>
                    <td>27</td>
                </tr>
                <tr onmouseover="changeColor('tr4','over')" id="tr4" onmouseout="changeColor('tr4','out')">
                    <td>4</td>
                    <td>赵六</td>
                    <td>29</td>
                </tr>
                <tr onmouseover="changeColor('tr5','over')" id="tr5" onmouseout="changeColor('tr5','out')">
                    <td>5</td>
                    <td>田七</td>
                    <td>30</td>
                </tr>
                <tr onmouseover="changeColor('tr6','over')" id="tr6" onmouseout="changeColor('tr6','out')">
                    <td>6</td>
                    <td>汾九</td>
                    <td>20</td>
                </tr>
            </tbody>
        </table>
    </body>
</html>
 

效果图:

 

 三、使用checkbox实现全选框和全不选框

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>全选和全不选</title>
        <script>
            function checkAll(){
                //1.获取编号前面的复选框
                var checkAllEle = document.getElementById("checkAll");
                //2.对编号前面复选框的状态进行判断
                if(checkAllEle.checked==true){
                    //3.获取下面所有的复选框
                    var checkOnes = document.getElementsByName("checkOne");
                    //4.对获取的所有复选框进行遍历
                    for(var i=0;i<checkOnes.length;i++){
                        //5.拿到每一个复选框,并将其状态置为选中
                        checkOnes[i].checked=true;
                    }
                }else{
                    //6.获取下面所有的复选框
                    var checkOnes = document.getElementsByName("checkOne");
                    //7.对获取的所有复选框进行遍历
                    for(var i=0;i<checkOnes.length;i++){
                        //8.拿到每一个复选框,并将其状态置为未选中
                        checkOnes[i].checked=false;
                    }
                }
            }
        </script>
        
    </head>
    <body>
        <table border="1" width="500" height="50" align="center" >
            <thead>
                <tr>
                    <td colspan="4">
                        <input type="button" value="添加" />
                        <input type="button" value="删除" />
                    </td>
                </tr>
                <tr>
                    <th><input type="checkbox" onclick="checkAll()" id="checkAll"/></th>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                </tr>
            </thead>
            <tbody>
                <tr >
                    <td><input type="checkbox" name="checkOne"/></td>
                    <td>1</td>
                    <td>张三</td>
                    <td>22</td>
                </tr>
                <tr >
                    <td><input type="checkbox" name="checkOne"/></td>
                    <td>2</td>
                    <td>李四</td>
                    <td>25</td>
                </tr>
                <tr >
                    <td><input type="checkbox" name="checkOne"/></td>
                    <td>3</td>
                    <td>王五</td>
                    <td>27</td>
                </tr>
                <tr >
                    <td><input type="checkbox" name="checkOne"/></td>
                    <td>4</td>
                    <td>赵六</td>
                    <td>29</td>
                </tr>
                <tr >
                    <td><input type="checkbox" name="checkOne"/></td>
                    <td>5</td>
                    <td>田七</td>
                    <td>30</td>
                </tr>
                <tr >
                    <td><input type="checkbox" name="checkOne"/></td>
                    <td>6</td>
                    <td>汾九</td>
                    <td>20</td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

效果图:

 四、使用onchange事件完成省市二级联动

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>省市二级联动</title>
        <script>
        //1.创建一个二维数组用于存储省份和城市
            var cities = new Array(3);
            cities[0] = new Array("武汉市","黄冈市","襄阳市","荆州市");
            cities[1] = new Array("长沙市","郴州市","株洲市","岳阳市");
            cities[2] = new Array("石家庄市","邯郸市","廊坊市","保定市");
            cities[3] = new Array("郑州市","洛阳市","开封市","安阳市");
            
            function changeCity(val){
                
                //7.获取第二个下拉列表
                var cityEle = document.getElementById("city");
                
                //9.清空第二个下拉列表的option内容
                cityEle.options.length=0;
                
                //2.遍历二维数组中的省份
                for(var i=0;i<cities.length;i++){
                    //注意,比较的是角标
                    if(val==i){
                        //3.遍历用户选择的省份下的城市
                        for(var j=0;j<cities[i].length;j++){
                            //alert(cities[i][j]);
                            //4.创建城市的文本节点
                            var textNode = document.createTextNode(cities[i][j]);
                            //5.创建option元素节点
                            var opEle = document.createElement("option");
                            //6.将城市的文本节点添加到option元素节点
                            opEle.appendChild(textNode);
                            //8.将option元素节点添加到第二个下拉列表中去
                            cityEle.appendChild(opEle);
                        }
                    }
                }
            }
        
        </script>
    </head> 
    <body>
        <form action="#" method="get" onsubmit="return check()">
            隐藏字段:<input type="hidden" name="id" value="" /><br />
            用户名:<input type="text" name="username" id="username" placeholder="请输入用户名"/><br />
            密码:<input type="password" name="password" id="password"/><br />
            确认密码:<input type="password" name="repassword" id="repassword"/><br />
            性别:<input type="radio" name="sex" value="男"/><input type="radio" name="sex" value="女" checked="checked"/><br />
            爱好:<input type="checkbox" name="hobby" value="钓鱼"/>钓鱼
            <input type="checkbox" name="hobby" value="打电动"/>打电动
            <input type="checkbox" name="hobby" value="写代码" checked="checked"/>写代码<br />
            头像:<input type="file" name="file"/><br />
            籍贯:<select onchange="changeCity(this.value)">
                                    <option>--请选择--</option>
                                    <option value="0">湖北</option>
                                    <option value="1">湖南</option>
                                    <option value="2">河北</option>
                                    <option value="3">河南</option>
                                </select>
                                <select id="city">
                                    
                                </select>
            <br />
            自我介绍:
                <textarea name="zwjs">
                    
                </textarea><br />
            提交按钮:<input type="submit" value="注册"/><br />
            普通按钮:<input type="button" value="注册"/><br />
            重置按钮:<input type="reset" />
        </form>
        
    </body>
</html>

效果图:

 

 

 

 

 

posted @ 2020-02-04 21:16  MoooJL  阅读(193)  评论(0编辑  收藏  举报