亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于javascipt-dom編程 table對(duì)象的使用

 更新時(shí)間:2013年04月22日 14:34:05   作者:  
本篇文章小編為大家介紹,基于javascipt-dom編程 table對(duì)象的使用。需要的朋友參考下

排名練習(xí):向表格添加數(shù)據(jù),當(dāng)編號(hào)遇到重復(fù)給予提示并且無法添加,而且按排名順序添加數(shù)據(jù)

參考代碼:

復(fù)制代碼 代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>ggggg</title>
<link rel="stylesheet" type="text/css" href="">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<!--js代碼可以放置在任意位置,按照先后順序依次執(zhí)行 一般放在head標(biāo)簽之間-->
<script type="text/javascript">

    function test(){

        //獲得表格對(duì)象
        var mytable=document.getElementById("table1");
        //判斷插入是否重復(fù)的排名
        //遍歷
        //定義變量要插入的位置
        var insertW=mytable.rows.length;
        for(var i=1;i<mytable.rows.length;i++){
            //取出每一行
            var eRows=mytable.rows[i];
            //判斷輸入的排名是否重復(fù)
            if(eRows.cells[0].innerText==no.value){
                window.alert("與排名 "+eRows.cells[0].innerText+" 重復(fù)");
                return ;//阻止代碼往下面執(zhí)行
            }

            //進(jìn)行排序 查找要插入的位置
            if(parseInt(no.value)>parseInt(eRows.cells[0].innerText)){
                insertW=i;
                //window.alert(insertW);
            }
            //window.alert("ok");
        }

       
        //排序后的插入
        var eachRow=mytable.insertRow(insertW+1);   
        //每行添加數(shù)據(jù)
        eachRow.insertCell(0).innerText=document.getElementById("no").value;
        eachRow.insertCell(1).innerText=username.value;
        eachRow.insertCell(2).innerText=nickname.value;

    }

</script>
</head>
<body>
    <table id="table1" border="1">
        <tr><td>排名</td><td>姓名</td><td>外號(hào)</td></tr>
        <tr><td>1</td><td>宋江</td><td>及時(shí)雨</td></tr>
        <tr><td>2</td><td>盧俊義</td><td>玉麒麟</td></tr>
        <tr><td>10</td><td>test</td><td>test</td></tr>
    </table>
    <br/><br/>
    排名:<input id="no" type="text" name="no"><br/>
    姓名:<input id="username" type="text" name="username"><br/>
    外號(hào):<input id="nickname" type="text" name="nickname"><br/>
    <input type="button" value="添加" onclick="test()"><br/>
</body>
</html>

相關(guān)文章

最新評(píng)論