var tab = document.getElementById('tab')
var len = tab.getElementsByTagName('tr').length
var rownum = len<10?objName.substring(objName.length-1):objName.substring(objName.length-2)
}
其实如果你只是想要获取当前行号的话,没必要从id截取字符串,这样写:
<script language="javascript">
window.onload=function(){
var tab = document.getElementById('tab')
var objs = tab.getElementsByTagName('input')
for(var i=0i<objs.lengthi++){
objs[i].onclick=function(){
var rownum = this.parentNode.parentNode.rowIndex+1
alert("我所在的行号为:"+rownum)
}
}
}
</script>
<table border="1" id="tab" width="37%">
<tr>
<td width="68"><input type="text" value="点击我获取行号" /></td>
<td>x4</td>
</tr>
<tr>
<td width="68"><input type="text" value="点击我获取行号" /></td>
<td>14</td>
</tr>
<tr>
<td width="68"><input type="text" value="点击我获取行号" /></td>
<td>24</td>
</tr>
<tr>
<td width="68"><input type="text" value="点击我获取行号" /></td>
<td>34</td>
</tr>
<tr>
<td width="68"><input type="text" value="点击我获取行号" /></td>
<td>44</td>
</tr>
</table>
其实这个问题用css解决是最完美方案,完全不需要js代码,任意行的表格都可通杀:
<style>table tr:nth-child(3n+4) td {border-bottom:solid 2px #999}
</style>
<table width="200" border="0">
<tbody>
<tr><td>行号</td><td>内容</td></tr>
<tr><td>1</td><td> </td></tr>
<tr><td>2</td><td> </td></tr>
<tr><td>3</td><td> </td></tr>
<tr><td>4</td><td> </td></tr>
<tr><td>5</td><td> </td></tr>
<tr><td>6</td><td> </td></tr>
</tbody>
</table>