简体   繁体   English

在第一行中获取表格单元格值在javascript中单击

[英]Get table cell value on first rowclick in javascript

I am trying to get a value of a cell in a html table within JavaScript. 我试图在JavaScript中的html表中获取单元格的值。 I can do this when the row is clicked twice or when I click on a row and then another row but I need to get the cell value on first click 我可以在单击行两次或单击一行然后再单击另一行但我需要在第一次单击时获取单元格值时执行此操作

This is my code so far 到目前为止这是我的代码

var table = document.getElementById('ContentPlaceHolder1_htmltable');

if (table != null) {
    for (var i = 0; i < table.rows.length; i++) {
        for (var j = 0; j < table.rows[i].cells.length; j++)
            table.rows[i].cells[j].onclick = function () {
                cellvalue = this.innerHTML;
            };
    }
} 

This runs on an onclick event on the ASPxListBox object from devexpress I am using. 这是在我正在使用的ASPxListBox上的ASPxListBox对象上的onclick事件上运行的。

Note: I can't use third party libraries like jQuery for this. 注意:我不能使用像jQuery这样的第三方库。

Thanks 谢谢

The jsfiddle code posted by arcade Gandalf above does work, however, to fix my problem of getting the cell value on first click, the code needs to run when the page load is complete 上面由arcade Gandalf发布的jsfiddle代码可以解决我在第一次点击时获取单元格值的问题,代码需要在页面加载完成时运行

window.onload = getCellValueOfTable;

function getCellValueOfTable {
   jsFiddle code or my code posted above...
}

Then in the onclick event for the listbox or a table as I described in the question, just call the above function and that is all. 然后在列表框的onclick事件或我在问题中描述的表中,只需调用上面的函数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM