Heim  >  Artikel  >  Web-Frontend  >  JS获取表格内指定单元格html内容的方法_javascript技巧

JS获取表格内指定单元格html内容的方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:06:511179Durchsuche

本文实例讲述了JS获取表格内指定单元格html内容的方法。分享给大家供大家参考。具体如下:

下面的代码先通过表格对象的rows获得指定的行的所有单元格数组,然后定位指定的单元格,通过单元格的innerHTML属性获得单元格的html内容

<!DOCTYPE html>
<html>
<head>
<script>
function cell()
{
var x=document.getElementById('myTable').rows[0].cells;
alert(x[0].innerHTML);
}
</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>
<input type="button" onclick="cell()"
value="Alert first cell">
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn