js/jquery循环提取table单元格值
<table id="tbitem"> <tr><td>测试数据1</td></tr> <tr><td>测试数据2</td></tr> <tr><td>测试数据3</td></tr> <tr><td>测试数据4</td></tr> <tr><td>测试数据5</td></tr> <tr><td>测试数据6</td></tr> </table> <script type="text/javascript"> var table = document.getElementById("tbitem"); var td = table.getElementsByTagName("td"); var len = td.length; for(i=0;i<len;i++){ alert(td[i].innerHTML); } </script>
jquery 版的
$(document).ready(function(){
var st1r="";
$("td").each(function(){ st1r+=$(this).html()+",";alert( st1r)})
});
这个是通过判断里面是否有td ,如果有td,都提取里面的值
<!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>untitle</title> <script> window.onload=function(){ var b=document.getElementsByTagName('td').length; for(var i=1;i<=b;i++){ var a=document.getElementById('a'+i); alert(a.innerHTML); } } </script> </head> <body> <table> <tr> <td id="a1">aa</td> <td id="a2">bb</td> <td id="a3">cc</td> </tr> </table> </body> </html>