admin管理员组文章数量:1279057
<table>
<tr onClick = "alert(this.FirstTDValue);" >
<td>123</td>
<td>Hello</td>
<td>Goodbye</td>
</tr>
</table>
How would this.FirstTDValue be acplished so that alert would show '123', would it be efficient to request it at the TR or the Table level?
<table>
<tr onClick = "alert(this.FirstTDValue);" >
<td>123</td>
<td>Hello</td>
<td>Goodbye</td>
</tr>
</table>
How would this.FirstTDValue be acplished so that alert would show '123', would it be efficient to request it at the TR or the Table level?
Share Improve this question asked Aug 25, 2011 at 0:35 billbill 1,1612 gold badges8 silver badges9 bronze badges 1- I imagine you would have to give the first <td> an id so it knows where to look and take the values from. – RSM Commented Aug 25, 2011 at 0:39
5 Answers
Reset to default 6Table rows have a cells collection that is a live NodeList of the cells, so:
alert(this.cells[0].textContent || this.cells[0].innerText);
Or perhaps:
alert(this.cells[0].firstChild.data);
<tr>
elements have a cells
collection that lets you access the child <td>
elements:
this.cells[0].innerHTML
Something like...
<tr onClick = "getTD();" >
<td>123</td>
<td>Hello</td>
<td>Goodbye</td>
</tr>
<script type="text/javascript">
function getTD(){
alert(document.body.getElementsByTagName("td")[0]);
}
</script>
This is how I did it:
<script>
Element.prototype.FirstTDValue = function() {
for (var i = 0; i< this.childNodes.length; i++) {
if (this.childNodes[i].tagName == 'TD') {
return this.childNodes[i].innerHTML;
}
}
return ("No TD Values");
}
</script>
<table>
<tr onClick = "alert(this.FirstTDValue())" >
<td>123</td>
<td>Hello</td>
<td>Goodbye</td>
</tr>
</table>
jQuery Solution:
<tr onClick="alert( $(this).children().first().text() )" >
本文标签: javascriptHow to get value from TD column using thisvalueStack Overflow
版权声明:本文标题:javascript - How to get value from TD column using this.value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236458a2363114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论