日期:2014-05-16  浏览次数:20425 次

js对table执行增-删-改
<html>

<head>

<title>作业</title>
<script language="javascript" type="text/javascript">
    
    /*页面加载完成*/
    window.onload = function(){
        var etable = "<table border = '1' id='myTable'>"+
                        "<tr align = 'center'> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>爱好</th> <th>操作</th></tr>"+
                        "<tr> <td onclick='updStu(this)' name='付政委'>付政委</td>"+
                        " <td onclick='updStu(this)' name='男'>男</td>"+
                        " <td onclick='updStu(this)' name='24'>24</td>"+
                        " <td onclick='updStu(this)' name='编程、看电影'>编程、看电影</td> "+
                        "<td><span onclick='delStu(this)' style='cursor:pointer; color=red'>删除</span>"+
                        "</td></tr>"+
                        
                     "</table><hr/>"+
                     "增加新用户<br/>"+
                     "姓名:<input type='text' id='stuName'/><br/>"+
                     "性别:<lable><input type='radio' id='stuSex' name='stuSex' value='男' checked = 'checked'/>男</lable>"+
                     "      <lable><input type='radio' id='stuSex' name='stuSex' value='女'/>女</lable><br/>"+
                     "年龄:<select id='stuAge'>"+
                     "      </select><br/>"+
                     "爱好:<label><input type='checkbox' value='野游' name='stuhobby' id='stuhobby'>野游</label> "+
                     "      <label><input type='checkbox' value='逛街' name='stuhobb' id='stuhobby'>逛街</label>"+
                     "      <label><input type='checkbox' value='交友' name='stuhobby' id='stuhobby'>交友</label>"+
                     "      <label><input type='checkbox' value='其他' name='stuhobby' id='stuhobby'>其他</label><hr/>"+
                     "<input type='button' value='新增用户' id='addStu'/>"
                     ;
        document.body.innerHTML = etable;
        
        
        /*向年龄中插入元素*/
        for(var i = 10; i<=100; i++){
            var eOption = document.createElement("option");
            eOption.value = i;
            eOption.text = i+"岁";
            $("stuAge").add(eOption);
        }
        
        /*提取用户信息,并增加到列表*/
        $("addStu").onclick = function(){
        
            
             /*提取用户名*/
             var stu_name = $("stuName").value;
             
             /*提取性别*/
             var stu_sexs = document.getElementsByName("stuSex"),stu_sex = "";
             for(var sid = 0,slen= stu_sexs.length; sid < slen; sid ++){
                if(stu_sexs[sid].checked){
                    stu_sex = stu_sexs[sid].value;
                }
             }
             
             /*获得年龄*/
             var stu_age = $("stuAge");
             stu_age = stu_age.options[stu_age.selectedIndex].value;
            
             /*获得爱好*/
             var stu_hobbys = document.getElementsByName("stuhobby"),stu_hobby="";   
             
            
             for(var shid = 0,shlen = stu_hobbys.length; shid < shlen; shid ++){
                    if(stu_hobbys[shid].checked){
                        stu_hobby += stu_hobbys[shid].value +"、";
                    }
             }
            
            /*把信息写入表格*/
            var trow = $("myTable").insertRow($("myTable").rows.length);
            //根