JS中獲取table節點的tr或td的內容


<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> 

  

接下來對該表格中的信息進行獲取:
<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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM