js中擷取 table節點各tr及td的內容方法__js

來源:互聯網
上載者:User
<table id="tb1" width="200" border="1" cellpadding="4" cellspacing="0">        <tr>                <td height="25">第一行</td>        </tr>        <tr>                <td height="25">第二行</td>        </tr>        <tr>                <td height="25">第三行</td>        </tr>        <tr>                <td height="25">第四行</td>        </tr> <tr>                <td height="25"><input type="button" name="getTableContent" value="獲得表格內容" onclick="getTableContent(this)"></td>        </tr></table>



如上所示代碼,按鈕點擊事件中傳入this對象,此對象就是按鈕本身。通過以下方法測試擷取表格的資料

<script language="javascript">        function getTableContent(node) {
// 按鈕的父節點的父節點是tr。    var tr1 = node.parentNode.parentNode;alert(tr1.rowIndex);alert(tr1.cells[0].childNodes[0].value); //擷取的方法一
                        alert(tr1.cells[0].innerText);

// 通過以下方式找到table對象,在擷取tr,td。然後擷取td的html內容                var table = document.getElementById("tb1");//擷取第一個表格                var child = table.getElementsByTagName("tr")[rowIndex - 1];//擷取行的第一個儲存格                var text = child.firstChild.innerHTML;                window.alert("表格第" + rowIndex + "的內容為: " + text);        }</script>





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.