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动态生成的。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值