Javascript从html表的特定行中获取列的文本值 [英] Javascript get the text value of a column from a particular row of an html table

查看:16
本文介绍了Javascript从html表的特定行中获取列的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户单击表格的一行,我想(在 Javascript 中)获取该行的第 3 列的 innerhtml.

类似:

document.getElementById("tblBlah").rows[i].columns[j].innerHTML

似乎无法实现,我在这里或网上找不到任何东西.

非常感谢任何解决方案(没有 jQuery)

解决方案

document.getElementById("tblBlah").rows[i].columns[j].innerHTML;

应该是:

document.getElementById("tblBlah").rows[i].cells[j].innerHTML;

但我有一个明显的印象,即您需要的行/单元格就是用户点击的那一行.如果是这样,实现此目的的最简单方法是将事件附加到表格中的单元格:

函数alertInnerHTML(e){e = e ||window.event;//IE警报(this.innerHTML);}var theTbl = document.getElementById('tblBlah');for(var i=0;i

这使得所有表格单元格都可以点击,并提醒它是innerHTML.事件对象将被传递给 alertInnerHTML 函数,其中 this 对象将是对被点击的单元格的引用.事件对象为您提供了大量关于您希望单击事件如何表现的巧妙技巧,例如,如果单元格中有一个被单击的链接,但我建议检查 MDN 和 MSDN(对于 window.event 对象)

A user clicks on a row of a table, and I want to get (in Javascript) the innerhtml of let's say the 3rd column of that row.

Something like :

document.getElementById("tblBlah").rows[i].columns[j].innerHTML

doesn't seem achievable and I can't find anything here or in the net.

Any solutions would be very much appreciated ( NO jQuery )

解决方案

document.getElementById("tblBlah").rows[i].columns[j].innerHTML;

Should be:

document.getElementById("tblBlah").rows[i].cells[j].innerHTML;

But I get the distinct impression that the row/cell you need is the one clicked by the user. If so, the simplest way to achieve this would be attaching an event to the cells in your table:

function alertInnerHTML(e)
{
    e = e || window.event;//IE
    alert(this.innerHTML);
}

var theTbl = document.getElementById('tblBlah');
for(var i=0;i<theTbl.length;i++)
{
    for(var j=0;j<theTbl.rows[i].cells.length;j++)
    {
        theTbl.rows[i].cells[j].onclick = alertInnerHTML;
    }
}

That makes all table cells clickable, and alert it's innerHTML. The event object will be passed to the alertInnerHTML function, in which the this object will be a reference to the cell that was clicked. The event object offers you tons of neat tricks on how you want the click event to behave if, say, there's a link in the cell that was clicked, but I suggest checking the MDN and MSDN (for the window.event object)

这篇关于Javascript从html表的特定行中获取列的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆