未捕获引用错误:未定义ShowTable.但我已经做了这个功能

Uncaught Reference Error: ShowTable is not defined . But i have already made that function

本文关键字:功能 引用 错误 ShowTable 未定义      更新时间:2023-09-26

我正在通过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()函数

尝试对类似dynamically added elements的使用on()

 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);        
});