日期:2014-05-16  浏览次数:20445 次

如何遍历获取table内的tr的属性
如何遍历获取table内的tr的属性
<table width="99%" border="0" cellspacing="1" cellpadding="0">
  <tr id="32543">
  <td>&nbsp;</td>
  </tr>
  <tr id="322543">
  <td>&nbsp;</td>
  </tr>
  <tr id="332443">
  <td>&nbsp;</td>
  </tr>
</table>
遍历获取tr的id
谢谢

------解决方案--------------------
JScript code

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="blog.anchen8.net" />

    <title>无标题 1</title>

</head>

<body>
<table width="99%" border="0" cellspacing="1" cellpadding="0">
  <tr id="32543">
  <td>&nbsp;</td>
  </tr>
  <tr id="322543">
  <td>&nbsp;</td>
  </tr>
  <tr id="332443">
  <td>&nbsp;</td>
  </tr>
</table>
</body>
<script>
var obj_tr=document.getElementsByTagName("tr");
var idList=new Array();
for(var i=0;i<obj_tr.length;i++){
    idList[i]=obj_tr[i].getAttribute("id");
}
for(var j=0;j<idList.length;j++){
    var obj_p=document.createElement("p");
    obj_p.innerHTML=idList[j];
    document.body.appendChild(obj_p);
}
</script>
</html>

------解决方案--------------------
<script type="text/javascript">
function get_id(){
var table=document.getElementsByTagName("table")[0];
var trs=table.getElementsByTagName("tr");
for(var i=0;i<trs.length;i++){
trs[i].childNodes[0].innerHTML="id="+trs[i].getAttribute("id");
}
}
window.onload=get_id;