亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

我們將學(xué)習(xí)如何使用JavaScript dom向表格中添加一行。為了實(shí)現(xiàn)這個(gè)目標(biāo),我們有多種方法。其中一些如下:

    使用 insertRow() 方法

    通過創(chuàng)建新元素

    使用 insertRow() 方法

    使用insertRow()方法可以在表格的開頭插入新行。它創(chuàng)建一個(gè)新的

    元素并將其插入到表格中。它接受一個(gè)數(shù)字作為參數(shù),指定表格的位置。如果我們不傳遞任何參數(shù),則在表格的開頭插入行。如果您想在表格的最后插入行,則將-1作為參數(shù)傳遞。

    語法

    table.insertRow(index)

    登錄后復(fù)制

    返回值 ? 被插入的元素。

    正如我們所知,一個(gè)合適的表格不僅有表格行(<tr>),還有被稱為表格單元格的表格文檔(<td>)。為了在行內(nèi)插入單元格,我們使用insertCell()方法。它在表格行內(nèi)創(chuàng)建一個(gè)<td>元素。它接受一個(gè)數(shù)字作為參數(shù),指定該行內(nèi)單元格的索引。

    語法

    下面是插入單元格的語法:

    table.insertCell(index)
    

    登錄后復(fù)制

    返回值 ? 被插入的元素。

    將一行添加到表格的步驟

      獲取數(shù)據(jù)表元素。

      使用insertRow方法創(chuàng)建一行,并將其插入到表格中。

      使用insertCell方法創(chuàng)建新的單元格,并將其插入到您創(chuàng)建的行中。

      向新創(chuàng)建的單元格中添加數(shù)據(jù)。

      Example

      的翻譯為:

      示例

      在此示例中,我們有一個(gè)包含學(xué)生姓名及其年齡的表。我們將在表格末尾添加一名新學(xué)生。

      <!DOCTYPE html>
      <html>
      <head>
         <title> Example- add rows to a table using JavaScript DOM </title>
         <style>
            table,
            td,
            th {
               border: 1px solid black;
            }
         </style>
      </head>
      <body>
         <h2> Adding rows to a table using JavaScript DOM </h2>
         <p> Click on the button to add the row to the table </p>
         <button id="btn" onclick="addRow()"> Click me </button>
         <br><br>
         <table id="myTable">
            <thead>
               <th> Name </th>
               <th> Age </th>
               <th> City </th>
            </thead>
            <tbody>
               <tr>
                  <td> Alex </td>
                  <td> 20 </td>
                  <td> New York </td>
               </tr>
               <tr>
                  <td> Tim </td>
                  <td> 18 </td>
                  <td> Boston </td>
               </tr>
               <tr>
                  <td> Mark </td>
                  <td> 23 </td>
                  <td> San Diego </td>
               </tr>
            </tbody>
         </table>
      </body>
      <script>
         function addRow() {
            // Get the table element in which you want to add row
            let table = document.getElementById("myTable");
         
            // Create a row using the inserRow() method and
            // specify the index where you want to add the row
            let row = table.insertRow(-1); // We are adding at the end
         
            // Create table cells
            let c1 = row.insertCell(0);
            let c2 = row.insertCell(1);
            let c3 = row.insertCell(2);
         
            // Add data to c1 and c2
            c1.innerText = "Elon"
            c2.innerText = 45
            c3.innerText = "Houston"
         }
      </script>
      </html>
      

      登錄后復(fù)制

      通過創(chuàng)建新元素

      在這種方法中,我們將使用document.createElement()方法創(chuàng)建新的行和列。

      方法

      以下是通過創(chuàng)建元素向表格添加行的步驟。

        獲取要添加行的表體元素

        創(chuàng)建行元素

        創(chuàng)建單元格將數(shù)據(jù)插入單元格

        將單元格添加到行中

        追加行到表格主體

        Example

        的翻譯為:

        示例

        <html>
        <head>
           <title> Example- add rows to a table using JavaScript DOM </title>
           <style>
              table,
              td,
              th {
                 border: 1px solid black;
              }
           </style>
        </head>
        
        <body>
           <h2> Adding rows to a table using JavaScript DOM </h2>
           <p>Click on the button to add the row to the table </p>
           <button id="btn" onclick="addRow()"> Click me </button>
           <br><br>
           <table id="myTable">
              <thead>
                 <th> Name </th>
                 <th> Age </th>
                 <th> City </th>
                 <th> Course </th>
              </thead>
              <tbody id="tableBody">
                 <tr>
                    <td> Alex </td>
                    <td> 20 </td>
                    <td> New York </td>
                    <td> Java </td>
                 </tr>
                 <tr>
                    <td> Tim </td>
                    <td> 18 </td>
                    <td> Boston </td>
                    <td> Python </td>
                 </tr>
                 <tr>
                    <td> Mark </td>
                    <td> 23 </td>
                    <td> San Diego </td>
                    <td> JavaScript </td>
                 </tr>
              </tbody>
           </table>
        </body>
        <script>
           function addRow() {
              // Get the table body element in which you want to add row
              let table = document.getElementById("tableBody");
           
              // Create row element
              let row = document.createElement("tr")
              
              // Create cells
              let c1 = document.createElement("td")
              let c2 = document.createElement("td")
              let c3 = document.createElement("td")
              let c4 = document.createElement("td")
              
              // Insert data to cells
              c1.innerText = "Elon"
              c2.innerText = "42"
              c3.innerText = "Houston"
              c4.innerText = "C++"
              
              // Append cells to row
              row.appendChild(c1);
              row.appendChild(c2);
              row.appendChild(c3);
              row.appendChild(c4);
              
              // Append row to table body
              table.appendChild(row)
           }
        </script>
        </html>
        

        登錄后復(fù)制

        以上就是如何使用JavaScript DOM向表格添加行?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!

分享到:
標(biāo)簽:DOM javascript 如何使用 添加 表格
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定