日期:2014-05-16 浏览次数:20373 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<div id="content_left">
<table>
<tbody>
<tr>
<td>
<a href="www.baidu.com">baidu</a>
</td>
</tr>
<tr>
<td>
<a href="www.google.com">google</a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<script type="text/javascript">
var nav_div = document.getElementById('content_left');
var s = '';
var nav_table=nav_div.getElementsByTagName('table');
for(var i = 0;i<nav_table.length;i++){
var nav_cells = nav_table[i].rows[0].cells;
var nav_a = nav_cells[0].getElementsByTagName('Anchor');
s = s + nav_a[0].href;
}
alert(s);
</script>
我试图得到表格里的链接地址,但是运行说对象nav_a为null或者未定义
<script type="text/javascript">
var nav_div = document.getElementById('content_left');
var linkArray = [];
var nav_table = nav_div.getElementsByTagName('table');
for (var i = 0; i < nav_table.length; i++) {
var s_table = nav_table[i];
for (var r = 0; r < s_table.rows.length; r++) {
var row = s_table.rows[r];
for (var c = 0; c < row.cells.length; c++) {
var cell = row.cells[c];
var a = cell.getElementsByTagName('a')[0];
if (a != null) {
linkArray.push(a.href);
}
}
}
}
alert(linkArray);
</script>
var nav_a = nav_cells[0].getElementsByTagName('Anchor'); // change to
var nav_a = nav_cells[0].getElementsByTagName('a');
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<script type="text/javascript">
var nav_div = document.getElementById('content_left');
var s = '';
var nav_table = nav_div.getElementsByTagName('table');
for (var i = 0; i < nav_table.length; i++) {
&