2015-09-04 53 views
0

我試圖打印頁面表中每一行的rowIndex。我想它被放置在表本身裏面,但我似乎無法得到它的工作:頁面加載時返回行的rowIndex

<table> 
    <tr> 
     <td>r1.cell1</td> 
     <td>r1.cell2</td> 
     <td><script>document.write("rowindex = " + this.parentNode.rowIndex);</script></td> 
    </tr> 
    <tr> 
     <td>r2.cell1</td> 
     <td>r2.cell2</td> 
     <td><script>document.write("rowindex = " + this.parentNode.rowIndex);</script></td> 
    </tr> 
</table> 
+0

這在你的腳本的執行的情況下指的是窗口對象看到它的行動,而不是節點。你的解決方案必須是純JS還是可以使用jQuery等庫? – Louis

+0

Louis,可以使用jQuery – oewebby

回答

0

使用jQuery是文件被加載後,做一個簡單的事情。假設這是在整個頁面,你可以做這樣的事情

$('td:last-child').each(function(index, item) { $(item).html(index)}) 

唯一的表,可以用這個的jsfiddle http://jsfiddle.net/qurm4304/