给定一个Html的<table> 如何遍历它的每一个单元的内容,并判断内容是否是数字类型
比如有下面一个页面:<html>
<body>
<table id="zzm" border=1>
<tr>
<td>
5
</td>
<td>
16
</td>
</tr>
<td>
zzm
</td>
<td>
 
</td>
</tr>
</table>
</body>
</html> --------------------编程问答-------------------- javascript:
var table = document.getElementById("zzm");
for(var row = 0, row<table.rows.length; row++) {
for (var cell=0; cell<table.rows[row].cells.length; cell++) {
...
}
} --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
public class HelloWorld
{
static void Main(string[] args)
{
string strHtml = @"<html>
<body>
<table id=""zzm"" border=1>
<tr>
<td>
5
</td>
<td>
16
</td>
</tr>
<td>
zzm
</td>
<td>
 
</td>
</tr>
</table>
</body>
</html>";
strHtml = Regex.Replace(strHtml, "\n|\r", "");
MatchCollection mcTD = Regex.Matches(strHtml, "<td>(.*?)</td>");
int tdLength = mcTD.Count;
if (tdLength > 0)
{
for (int i = 0; i < tdLength; i++)
{
string strResults = mcTD[i].Groups[1].Value.Trim();
string str = "是数字";
try
{
int.Parse(strResults);
}
catch
{
str = "不是数字";
}
Console.WriteLine(strResults+str);
}
}
else
{
Console.WriteLine("没有匹配到");
}
Console.ReadKey();
}
}
如果是多个table先循环table,循环table和循环td同理! --------------------编程问答-------------------- 一种解决方法:完全用页面的javaScript实现
<html>
<body>
<table id="zzm" border=1>
<tr>
<td>
5
</td>
<td>
16
</td>
</tr>
<td>
zzm
</td>
<td>
 
</td>
</tr>
</table>
</body>
<Script Language ="JavaScript">
var myrows=document.all.zzm.rows.length;
var i,j;
for(i=0;i<myrows;i++){
for(j=0;j<2;j++){
var myvalue=document.getElementById("zzm").rows[i].cells[j].innerText;
//这句去除了空格字符,因为isNaN函数把空格也认成数字
var tmp = parseInt(myvalue);
if(!isNaN(tmp)){
alert("行:"+i+" 列:"+j+"值:"+myvalue);
}
else
{ alert ("fail");}
}
}
//alert(document.all.zzm.rows[0].cells.length+"列");
//alert(myrows+"行");
//alert(document.getElementById("zzm").rows[1].cells[1].innerText);
//alert(document.all.zzm.rows[0].cells[1].innerText);
</Script>
</html>
补充:.NET技术 , C#