在html中有<div id="tableContainer">
我在其中附加了动态创建的表
table = $("<table id="myTable" class='helpTable'>");
table.append("<tr id='1'><td>content<td></tr>");code
table.append("<tr id='2'><td>content<td></tr>"); code
$('#tableContainer').append(table);
现在我想在表格行上添加事件,例如
$("#myTable tr").on('click',function(event) {
alert("Hi"); });
它不工作,但是以下情况正在工作
$("#tableContainer").on('click',function(event) {
alert("Hi"); });
即我可以在静态 html 标签上发生事件
最佳答案
您需要使用event delegation因为表和 tr 元素是动态创建的
$("#tableContainer").on('click', '#myTable tr', function (event) {
alert("Hi");
});
关于javascript - jquery : not able to select table elements which are created dynamically by jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643489/