我正在尝试获取动态创建的表的id。我从json对象获取的数据。我的问题与单击静态锚点时访问动态创建的表有关。这个锚标签不在动态创建的表中,它在同一页面的表格旁边。锚标签不是动态的。
当只在一个地方可用时,我不理解动态生成的表id'resTable'的范围。感谢有人能解释我在这里缺少的东西。
提前谢谢。我的占位符div标签如下所示。
$(document).ready(function() {
tableStr += "<div class=\"test-table\">";
tableStr += "<table id=\"resTable\">";
tableStr += "<thead><tr>";
$.each(parsedArray.data[0], function(key, value){
tableStr += '<th scope="col">' + key + '</th>';
});
tableStr += '</tr></thead><tbody>';
for (var i in parsedArray.data) {
if(i<parsedArray.data.length){
var row = 0;
tableStr += '<tr id=\"row'+i+'\">';
$.each(parsedArray.data[i], function(key, value){
tableStr += '<td>' + value + '</td>';
tableStr += '</tr>';
row++;
}
}
}
tableStr += '</tbody></table></div>';
var ele = document.getElementById('tableDiv');
ele.innerHTML = tableStr;
alert("html():"+$('#resTable').html());
//able to access here as soon as table dynamically created
$('#recordAnchor).click (function() {
alert("html():"+$('#resTable').html());
//NOT ABLE TO ACCESS 'resTable' that is dynamically generated under
//DiV id 'table Div'
}
}
//Unable to access from the below function. this function is called onClick of
//a button
<input type="button" name="nextRecord" onclick="getRecord()"/>
function getRecord(){
alert("html():"+$('#resTable').html());
//NOT ABLE TO ACCESS 'resTable' that is dynamically generated under
//DiV id 'table Div'
}
我的Html如下所示
<div class="bd">
<br>
<div class="uwidth">
<div id="tableDiv"></div>
</div>
<br>
<div class="buttonPadding">
<input id="nextRecordButton" name="nextRecordButton" onclick="getRecord()" type="button" value="Next"/>
</div>
<div id="recordAnchor">
<a href="#" title="[Action] [Unique ID]">Record View</a>
</div>
</div>