我使用此代码
通过java脚本动态生成链接 var infoHtml = "<a href=\"javascript:void(0);\" id=\"result-table-" + tableFor + postFixForTableId + "\" onclick=\"ShowTable(result-table-" + tableFor + postFixForTableId + ")\"> \
<p align=\"center\">For loop number: " + postFixForTableId + ", <span id=\"stuCnt" + postFixForTableId + "\"> \
<\/span> student(s) were " + message + "</p></a>";
并且jQuery将此代码附加到div上。此代码位于
之下var node = document.getElementById('node-id');
$('#node-id').append(infoHtml);
现在有了这个,我正确地获得了ShowTable()
函数与必需参数
制作显示表功能的代码是
function ShowTable(tableId) {
// blah .. blah
}
当我运行此控制台时出现错误Uncaught Reference Erroe: ShowTable is not defined
我的所有代码都在$(document).ready()
函数
答案 0 :(得分:0)
尝试将on()用于dynamically added elements
,
var infoHtml = "<a class="a-showtable" href=\"javascript:void(0);\" id=\"result-table-" + tableFor + postFixForTableId + "\" data-tableFor=" + tableFor + " data-postfix="+postFixForTableId + "\"> \
<p align=\"center\">For loop number: " + postFixForTableId + ", <span id=\"stuCnt" + postFixForTableId + "\"> \
<\/span> student(s) were " + message + "</p></a>";
并称之为,
$(document).on('click','.a-showtable',function(){
// you code to show table
// or call it like,
tableFor=$(this).data('tableFor');
postfix=$(this).data('postfix');
ShowTable('result-table-'+tableFor+postfix);
});