现在有个需求,对于一个页面上的<tr>中的文本,对于<td>中class相同的一列进行范围筛选,是为特定的日期格式和09/29-02:19:40的相隔时间为多少。思想就是找到<td>中的值,写了一个getElementsByClassName(n)的方法, 根据正则表达式跟文本值判断,然后写一个两位分隔的方法getNum(number),最后测试具体的间隔之后判断<td>的父节点<tr>显示隐藏或者显示。可以实现对于多少时间内的选择。
<html>
<head>
<script language="javascript" type="text/javascript">
function getElementsByClassName(n){
var obj = document.getElementsByTagName('td');
var el = [];
for(var i=0;i<obj.length;i++){
if(obj[i].className == n){
el[el.length] = obj[i];
}
}
return el;
}
function getNum(number){
var mag = new Array();
var word;
var i = number.length;
while(i>0){
word = number.substring(i,i-2);
i-=2;
mag.unshift(word);
}
number = mag;
return number;
}
function test(id){
var test = getElementsByClassName('3');
var obj = document.getElementById(id);
var test1 = new Array();
var test2 = new Array();
for(var i=0;i<test.length;i++){
test1[i] = test[i].innerText.replace(/[^0-9]/ig,"");
test2[i] = getNum(test1[i]);
var iMonth = test2[i][0] - 9;
var iDay = test2[i][1] - 29;
var iHours = test2[i][2] - 2;
var iMinutes = test2[i][3] - 19;
var iSeconds = test2[i][4] - 40;
var interval = iMonth*30*24*60*60+iDay*24*60*60+iHours*60*60+iMinutes*60+iSeconds;
if(interval<obj.value){
test[i].parentNode.style.display = "block";
}
if(interval>=obj.value){
test[i].parentNode.style.display = "none";
}
}
}
</script>
</head>
<body>
<table>
<select οnchange="test(this.value);">
<option value="" id = "">---time---</option>
<option value="2" id = "2">2seconds</option>
<option value="3" id = "3">3seconds</option>
<option value="5" id = "5">5seconds</option>
<option value="5" id = "all">all</option>
</select>
<tr ><td class='3'>09/29-02:19:40</td><td>ooo</td></tr>
<tr ><td class='3'>09/29-02:19:41</td><td>oto</td></tr>
<tr ><td class='3'>09/29-02:19:42</td><td>oto</td></tr>
<tr ><td class='3'>09/29-02:19:43</td><td>oto</td></tr>
<tr ><td class='3'>09/29-02:19:44</td><td>oto</td></tr>
<tr ><td class='4'>ccc </td><td>nnn</td></tr>
</table>
</body>
</html>