Javascript 单击html表格单元格时如何获取其内部的数据?
我创建了一个html表并动态填充它。因此,我希望在单击表的单元格时,对该单元格中的数据发出警报。这是表格的代码 JSP 您可以使用Javascript 单击html表格单元格时如何获取其内部的数据?,javascript,jquery,html-table,Javascript,Jquery,Html Table,我创建了一个html表并动态填充它。因此,我希望在单击表的单元格时,对该单元格中的数据发出警报。这是表格的代码 JSP 您可以使用$(this).text()获取表格单元格中的数据。 实际上,您正试图提醒“td”对象,而不是来自该对象的值,因此您可以使用以下方法来解决查询。由于动态添加了数据,因此我使用on而不是直接单击函数 var choice = $('table#load-opt tr td'); choice.on('click', function(){ alert($(this)
$(this).text()
获取表格单元格中的数据。
实际上,您正试图提醒“td”对象,而不是来自该对象的值,因此您可以使用以下方法来解决查询。由于动态添加了数据,因此我使用on而不是直接单击函数
var choice = $('table#load-opt tr td');
choice.on('click', function(){
alert($(this).text());
});
使用$(this).text()
使用警报($(this))而不是警报(choice),变量选项在此范围内未定义。
var fillTable = function($element, data) {
var t_head = $("<tr></tr>");
for(var ind=0; ind<data.length; ind++) {
//name is the name and desc is the description for each option
var $option = $("<tr></tr>");
$option.html("<td>"+name+"</td><td>"+desc+"</td>");
}
};
var choice = $('table#load-opt tr td');
choice.click(function(){
alert(choice); // also tried alerting choice.textContent, choice.innerHTML and choice.firstChild
});
var choice = $('table#load-opt tr td');
choice.on('click', function(){
alert($(this).text());
});