admin管理员组文章数量:1394088
My table can get row index by .rowIndex. Is it possible if I want third column data of row.
My Table :
<table width="90%" border="1" id="TestAlert">
<tbody>
<tr><td>Attribute</td> <td>Thai</td> <td>English</td></tr>
<tr onclick="myFunction((this))"><td>น้ำหนัก</td><td>...</td><td>WT</td></tr>
<tr onclick="myFunction((this))"><td>ชื่อรุ่น</td><td>...</td><td>MDL</td></tr>
<tr onclick="myFunction((this))"><td>สูง</td><td>...</td><td>HIGH</td></tr></tbody>
</table>
and my script:
function myFunction(x) {
if(x.rowIndex != 0){
console.log("Row index is: " + x.rowIndex + "Data 3rd cell: " + **CellData**); //get index
}
What function should I use get 'WT' or 'MDL' or 'HIGH' when i click each row?
My table can get row index by .rowIndex. Is it possible if I want third column data of row.
My Table :
<table width="90%" border="1" id="TestAlert">
<tbody>
<tr><td>Attribute</td> <td>Thai</td> <td>English</td></tr>
<tr onclick="myFunction((this))"><td>น้ำหนัก</td><td>...</td><td>WT</td></tr>
<tr onclick="myFunction((this))"><td>ชื่อรุ่น</td><td>...</td><td>MDL</td></tr>
<tr onclick="myFunction((this))"><td>สูง</td><td>...</td><td>HIGH</td></tr></tbody>
</table>
and my script:
function myFunction(x) {
if(x.rowIndex != 0){
console.log("Row index is: " + x.rowIndex + "Data 3rd cell: " + **CellData**); //get index
}
What function should I use get 'WT' or 'MDL' or 'HIGH' when i click each row?
Share Improve this question edited Nov 8, 2017 at 3:22 patipat chewprecha asked Nov 8, 2017 at 3:08 patipat chewprechapatipat chewprecha 2852 gold badges7 silver badges21 bronze badges 02 Answers
Reset to default 5No need of adding onclick
event handler to every tr
.Rather in js attach onclick
to tbody
.
On click
get the event object & from there get the target. The target will be td
.innerHTML
will give the content of the td & parentNode
will return the tr
of the td
from where the event generated.
Use rowIndex
to get the index of the row. rowIndex
starts from 0
var table = document.getElementById('TestAlert')
var getTBody = table.getElementsByTagName("tbody")[0];
getTBody.onclick = function(e) {
console.log(e.target.parentNode.rowIndex)
console.log(e.target.innerHTML)
};
<table width="90%" border="1" id="TestAlert">
<tbody>
<tr>
<td>Attribute</td>
<td>Thai</td>
<td>English</td>
</tr>
<tr>
<td>น้ำหนัก</td>
<td>...</td>
<td>WT</td>
</tr>
<tr>
<td>ชื่อรุ่น</td>
<td>...</td>
<td>MDL</td>
</tr>
<tr>
<td>สูง</td>
<td>...</td>
<td>HIGH</td>
</tr>
</tbody>
</table>
function getAllItems(){
const items = document.querySelectorAll('tr');
const itemsArr = Array.from(items);
itemsArr.forEach(c=> console.log(c.textContent));
}
getAllItems();
Now on your tr element do the following:
<tr onclick="getAllItems()"><td>สูง</td><td>...</td><td>HIGH</td></tr>
</tbody>
本文标签: htmlJavascript can get row index and get cell data at same timeStack Overflow
版权声明:本文标题:html - Javascript can get row index and get cell data at same time? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744598562a2614927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论