Table cells 集合

Table 對象參考手冊 Table 對象

定義和用法

cells 集合返回表格中所有 <td> 或 <th> 元素。

語法

tableObject.cells

屬性

屬性 描述
length 返回集合中 <td> 或 <th> 元素的數目。

方法

方法 描述
[name_or_index] 一個整數,指定元素檢索位置(從0開始)
item(name_or_index) 返回集合中的指定索引的元素
namedItem(name) 返回集合中指定名稱索引的元素索引


流覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要流覽器都支持 cells 集合


實例

實例

下麵的例子使用 cells 來顯示出第一個單元格的內容:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function displayResult(){
    alert(document.getElementById("myTable").rows[0].cells.length);
}
</script>
</head>
<body>

<table id="myTable" border="1">
    <tr>
        <td>cell 1</td>
        <td>cell 2</td>
    </tr>
    <tr>
        <td>cell 3</td>
        <td>cell 4</td>
    </tr>
</table>
<br>
<button type="button" onclick="displayResult()">顯示第一行單元格的數量</button>

</body>
</html>



實例

更多實例


Table 對象參考手冊 Table 對象