每一行都一个按钮,点击这个按钮,在此按钮所在的行的下面增加一行?js怎么写哦?

解决方案 »

  1.   


    <html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            <META http-equiv="Content-Style-Type" content="text/css">
        </head>
        <script language=javascript>
            var TH_HEIGHT="20px";
            var THCOLOR="#ffff99";
            var THBGCOLOR= "#ccecff";
            var NEW_COLOR="#ffffff";
            var rows=0;
            function createEventTable() {
                var newRow,col1,col2,col3;
                var ln = document.getElementById("tb1").rows.length;
                if( ln > 0){
                    for(var i=0; i<ln; i++){
                        if(document.getElementById("tb1").rows.length <= 0)break;
                        document.getElementById("tb1").deleteRow(-1);
                    }
                }
                for(var colno=0; colno<rows; colno++){    
                    newRow = document.getElementById("tb1").insertRow(-1);
                    newRow.id = 'r'+colno;
                    document.getElementById("r"+colno).height = TH_HEIGHT;
                    document.getElementById("r"+colno).bgColor = THCOLOR;
                    col1=newRow.insertCell(0);
                    col2=newRow.insertCell(1);
                    col3=newRow.insertCell(2);
                    col1.id="col1"+colno
                    col2.id = "col2"+colno;
                    col3.id = "col3"+colno;
                    col1.innerText = "列一"+colno;
                    col2.innerText = "列二"+colno;
                    col3.innerText = "列三"+colno;
                    col1.onclick=alertMe
                    col2.onclick=alertMe
                    col3.onclick=alertMe
                }
            }
            function alertMe(){
                alert("我的ID是"+this.id)
            }
            window.onload=function(){
             
            }
     
    function addline(){
    rows++;
    createEventTable();
    }
        </script>
        <body >
    <div><input type=button onclick=addline()  value="  +   ">
    </div>        <TABLE  cellSpacing="0" borderColorDark="black" cellPadding="1" borderColorLight="black"  border="1" ID="Table1">
                <thead>
                <COLGROUP>
                    <COL align="center">
                    </COL>
                    <COL align="center">
                    </COL>
                    <COL align="center">
                    </COL>
                    <COL align="center">
                    </COL>
                <TR height="22px" bgColor="#ccecff">
                    <TH width="36" >列1</TH>
                    <TH width="36" >列2</TH>
                    <TH width="36" >列3</TH>
                </TR>
            </thead>
            <TBODY id="tb1">
            </TBODY>
        </TABLE></body>
    </html>
      

  2.   


    <html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            <META http-equiv="Content-Style-Type" content="text/css">
            <script language="javascript">
                
                function addInput(){
                    var div=document.getElementById("inputDiv");
                    var input=document.createElement("input");
                    div.appendChild(input);
                }
                function decInput(){
                    var div=document.getElementById("inputDiv");
                    var nodeLen=div.childNodes.length;
                    if(div.childNodes[nodeLen-1]!=null&&nodeLen>4)                
                        div.removeChild(div.childNodes[nodeLen-1]);
                }
                    window.onload=function(){
                     
                    }
                        </script>
        </head>
        <body>
            <div id="inputDiv"><input type="text" /><input type="button" onclick="addInput()" value="+" ></inpit><input type="button" onclick="decInput()" value="-" ></inpit></div>
            <div></div>
        </body>
    </html>
      

  3.   

    都有一个+号的<html>
        <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            <META http-equiv="Content-Style-Type" content="text/css">
            <script language="javascript">
                
                function addInput(){
                    var div=document.getElementById("inputDiv")
                    var input="<input type=text ><input type=button onclick=addInput() value=\"+\">"
                    div.innerHTML+=input;
                    var br=document.createElement("br");
                    div.appendChild(br);
                }
              
                    window.onload=function(){
                     
                    }
                        </script>
        </head>
        <body>
            <div id="inputDiv"><input type="text" ID="Text1" NAME="Text1"/><input type="button" onclick="addInput()" value="+" ID="Button1" NAME="Button1"></input></br></div>
            <div></div>
        </body>
    </html>
      

  4.   


    <html>
    <head>
    <title></title>    
          <script  type="text/javascript">
          var i = 1 ;
    function add(){
    i++ ;
    var obj = event.srcElement;
    var tr = obj.parentNode.parentNode ;
    var tb = document.getElementById("tb") ;
    var row = tb.insertRow(tr.rowIndex+1) ;
    var cell1 = row.insertCell(0) ;
    cell1.innerHTML = i + "" ;
    var cell2 = row.insertCell(1) ;
    cell2.innerHTML = "<input type='button' value='添加' onclick='add()'>" ;
    } </script>
    </head>
    <body>
    <table id="tb" border="1">
    <tr>
    <td>id</td>
    <td>添加</td>
    </tr>
    <tr>
    <td>1</td>
    <td><input type="button" value="添加" onclick="add()"></td>
    </tr>
    </table>
    </body>
    </html>
      

  5.   

    帮你写了个简单的:<script>
    function Add_Tr(){
        var Tr = document.getElementById("tab").insertRow();
        var Td = Tr.insertCell();
        Td.innerHTML = "这是新增的一行";
    }
    </script>
    <table id="tab" border="1">
    <tr>
     <td>这是默认的啊</td>
    </tr>
    </table>
    <input type="button" value="添加一行" onclick="Add_Tr()" />