JavaScript method to get the value of a column in a table

  • 2020-03-30 02:50:58
  • OfStack

1, to achieve the source code
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>JavaScript Implement access table The value of a column in </title> 
<script type="text/javascript"> 
function getTdValue() 
{ 
var tableId = document.getElementById("tab"); 
var str = ""; 
for(var i=1;i<tableId.rows.length;i++) 
{ 
alert(tableId.rows[i].cells[1].innerHTML); 
} 
} 
</script> 
</head> 

<body style="width:100%; height:100%;"> 
<table style="width:100%; height:100%;" id="tab" onclick="getTdValue()" border="1" cellpadding="1" cellspacing="1"> 
<tr style="background-color:#CCC;"> 
<th> Student id </th> 
<th> The name </th> 
<th> gender </th> 
<th> age </th> 
</tr> 
<tr> 
<td>1</td> 
<td> rees </td> 
<td> male </td> 
<td>22</td> 
</tr> 
<tr> 
<td>2</td> 
<td> Wang qian </td> 
<td> female </td> 
<td>20</td> 
</tr> 
<tr> 
<td>3</td> 
<td> Yu Qian </td> 
<td> male </td> 
<td>18</td> 
</tr> 
<tr> 
<td>4</td> 
<td> liulang </td> 
<td> female </td> 
<td>19</td> 
</tr> 
<tr> 
<td>5</td> 
<td> The various ge is bright </td> 
<td> male </td> 
<td>20</td> 
</tr> 
<tr> 
<td>6</td> 
<td> The east cloud </td> 
<td> female </td> 
<td>21</td> 
</tr> 
<tr> 
<td>7</td> 
<td> Mr Sun ce </td> 
<td> male </td> 
<td>22</td> 
</tr> 
<tr> 
<td>8</td> 
<td> baoqing </td> 
<td> female </td> 
<td>23</td> 
</tr> 
<tr> 
<td>9</td> 
<td> Intellectual education </td> 
<td> male </td> 
<td>20</td> 
</tr> 
<tr> 
<td>10</td> 
<td> Wicker silk </td> 
<td> female </td> 
<td>21</td> 
</tr> 
</table> 
</body> 
</html> 

2. Design results

(1) initialization
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/201405060958271.gif? 2014469596 ">  
(2) when the second column is clicked
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/201405060959382.gif? 20144695957 ">

Related articles: