jquery 動態table

<html>
<head>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<style>
.buttonAdd{

}
</style>
</head>
<body>
<table  id ="contentTable" border="1">
                                  <tr>
                                     <td>
                                        <textarea class="textarea" name="content" id="content" cols="50" rows="3"></textarea>
                                     </td>
                                     <td id="add" class="pubt">
                                        <input class="buttonAdd" type="button" value="+" id="add" />
                                       </td>
                                        <td >
                                       </td>
                                  </tr>
                                  <tr>
                                     <td>
                                        <textarea class="textarea" name="content" id="content" cols="50" rows="3"></textarea>
                                     </td>
                                     <td id="add" class="pubt">
                                        <input class="buttonAdd" type="button" value="+" id="add" />
                                       </td>
                                        <td id="drop" class="pubt">
                                        <input class="buttonAdd" type="button" value="-" id="drop" />
                                       </td>
                                  </tr>
                                </table>

</body>


</html>
<script type="text/javascript">

$(document).ready(function() {
    $(".buttonAdd").live("click",function(){
        var trSeq = $(this).parent().parent().parent().find("tr").index($(this).parent().parent()[0]);
        if(this.id == "add"){
             var newRow = '<tr><td><textarea class="textarea" name="content" id="content" cols="50" rows="3"></textarea></td><td id="add" class="pubt"><input class="buttonAdd" type="button" value="+" id="add" /></td><td id="drop" class="pubt"><input class="buttonAdd" type="button" value="-" id="drop" /></td></tr>';
             $(newRow).insertAfter($("#contentTable tr:eq("+trSeq+")"));
        }else if(this.id == "drop"){
            $("#contentTable tr:eq("+trSeq+")").remove();
        }
       });
    });
    
    $(":button").click(function(){
        $("#contentTable td").click(function(){
            var trSeq = $(this).parent().parent().find("tr").index($(this).parent()[0]);
            if(this.id == "add"){
                 var newRow = '<tr><td><textarea class="textarea" name="content" id="content" cols="50" rows="3"></textarea></td><td id="add" class="pubt"><input class="button" type="button" value="+" id="addButton" /></td><td id="drop" class="pubt"><input class="button" type="button" value="-" id="dropButton" /></td></tr>';
                 $(newRow).insertAfter($("#contentTable tr:eq("+trSeq+")"));
            }else if(this.id == "drop"){
                $("#contentTable tr:eq("+trSeq+")").remove();
            }
            
            //var tdSeq = $(this).parent().find("td").index($(this)[0]);  
            ///var trSeq = $(this).parent().parent().find("tr").index($(this).parent()[0]);  
 
            //$("#contentTable tr:eq("+trSeq+")").remove();
            
            //$(row).insertAfter($("#table1 tr:eq("+rownum+")"));   
            
            
            //alert("第" + (trSeq + 1) + "行,第" + (tdSeq + 1) + "列");  
        });  
     });  

});


js動態生成的dom元素得用live進行事件委託,如
$(".t").live("click",function(){
    alert(this);
});
這樣樣式爲t的元素就能觸發click事件,不管是頁面加載完前生成的還是之後js動態生成的。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章