js在指定tr后面添加tr

方法(1)此方法不能在制定的位置添加

      <script language="javascript">  

  1. var table = document.getElementById("table_id");  
  2. var tr = table.insertRow();  
  3. for(var i=0; i<4; i++){  
  4.   var td = tr.insertCell(i);  
  5.   td.innerText = i;  
  6. }  
  7. </script>  
  8.   
  9. <table border=1 id="table_id">  
  10. </table>  

方法(2)实现

  function  onAddTR(trIndex)
         {
             var  tb = document.getElementById( "tb1" );
             var  newTr = tb.insertRow(trIndex); //添加新行,trIndex就是要添加的位置
             var  newTd1 = newTr.insertCell();
             newTd1.innerHTML =  "这是新行,位置:"  + trIndex;
             var  newTd2 = newTr.insertCell();
             newTd2.innerHTML =  "这是新行,位置:"  + trIndex;
         }
方法(3)jQuery实现
   < table  style = "width: 100%;"  id = "tb1" >
         < tr  onclick = "onAddTR(this)" >
             < td >1</ td >
             < td >2</ td >
         </ tr >
         < tr  onclick = "onAddTR(this)" >
             < td >3</ td >
             < td >4</ td >
         </ tr >
     </ table >
     < script  type = "text/javascript" >
         function onAddTR(trObj)
         {
             $(trObj).after("< tr  onclick = 'onAddTR(this)' >< td >这是新行</ td >< td ></ td >< tr />");搜索
         }
     </ script >

方法(4)dom实现在制定的位置前插入tr

原文:

<table> 

<tr  id="insertSpareEpt">
  <td colspan="24" align="right"><input  type="button" value="添加" οnclick="insertSpareEpt()"/></td>
  </tr>

</table>

脚本:
  <script type="text/javascript">
   function insertSpareEpt(){
   var insertSpareEpt=document.getElementById("insertSpareEpt");
   var newChild=document.createElement("tr");
   newChild.innerHTML=" <td colspan='2'><%=j%></td> <td colspan='8'><input type='text' name='spareEpt<%=j%>'> </td><td colspan='7'><input type='text'  name='spareEptStyle<%=j%>'></td><td colspan='2'><input type='text' name='spareEptUnit<%=j%>' size='14' /></td><td colspan='3'><input type='text'  name='spareEptNum<%=j %>' size='14'/></td> <td colspan='2' ><input type='button' value='删除' οnclick='deleteTrShow()'/></td>";
   //var newChild2=document.createElement("tr");
   //newChild.innerHTML="<td colspan='24' align='left'><input type='button' value='删除'/></td>";
    insertSpareEpt.parentNode.insertBefore(newChild,insertSpareEpt);
   }

下面是使用 jQuery 实现的代码: ```html <form> <label>用户名:<input type="text" name="username"></label><br> <label>密码:<input type="password" name="password"></label><br> <label>年龄:<input type="number" name="age"></label><br> <button type="button" id="submitBtn">提交</button> </form> <table> <thead> <tr> <th>用户名</th> <th>密码</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody id="recordList"> </tbody> </table> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> // 点击提交按钮时触发 $('#submitBtn').click(function() { let username = $('input[name="username"]').val(); let password = $('input[name="password"]').val(); let age = $('input[name="age"]').val(); // 发送 POST 请求到 submit.php,将表单数据提交 $.post('submit.php', { username, password, age }, function(data) { // 渲染表格中的记录 renderRecords(data); }); }); // 渲染表格中的记录 function renderRecords(records) { let tbody = $('#recordList'); tbody.empty(); // 清空表格内容 // 添加表头 let tr = $('<tr>').appendTo(tbody); $('<th>').text('用户名').appendTo(tr); $('<th>').text('密码').appendTo(tr); $('<th>').text('年龄').appendTo(tr); $('<th>').text('操作').appendTo(tr); // 添加记录 for (let i = 0; i < records.length; i++) { let record = records[i]; let tr = $('<tr>').appendTo(tbody); $('<td>').text(record.username).appendTo(tr); $('<td>').text(record.password).appendTo(tr); $('<td>').text(record.age).appendTo(tr); let td4 = $('<td>').appendTo(tr); $('<button>').text('删除').click(function() { // 发送 POST 请求到 delete.php,删除指定记录 $.post('delete.php', { index: i }, function(data) { // 渲染表格中的记录 renderRecords(data); }); }).appendTo(td4); } } // 页面加载时,从 get.php 获取已有记录并渲染 $.get('get.php', function(data) { renderRecords(data); }); </script> ``` 上面的代码中,我们使用了 jQuery 的 `$.post` 函数和 `$.get` 函数来发送 POST 和 GET 请求。`submit.php` 和 `delete.php` 的代码与上面使用 PHP 实现的代码相同,这里不再赘述。需要注意的是,在 `$.post` 函数和 `$.get` 函数中,第二个参数是提交的数据,可以使用对象字面量来简化代码。另外,`renderRecords` 函数也需要修改为接收参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值