javascript - 函数不是 onClick 中的函数
问题描述
我试图从每个 tr 调用一个内置函数,当它被点击时。但我得到一个错误Uncaught TypeError: this.updateSelection is not a function at HTMLTableRowElement.onclick (index.html:1)
。
在这里,我渲染了我的 HTML 模板。
this.renderHtml = function () {
return '<div class="widget-content widget-infotablewidget">' +
'<div class="infotable"></div>' +
'</div>';
};
当某些条件通过时,我会渲染一个表格。
this.updateTable = function (rows) {
let appendix = '<table><thead><tr>';
for (let i = 0; i < Object.keys(rows[0]).length; i++) {
appendix += '<th>' + Object.keys(rows[0])[i] + '</th>';
}
appendix += '</tr></thead><tbody>';
for (let i = 0; i < rows.length; i++) {
appendix += `<tr onclick="this.updateSelection(\'InfoTable\', ${i})">`;
for (let cell in rows[i]) {
appendix += '<td>' + rows[i][cell] + '</td>';
}
appendix += '</tr>';
}
appendix += '</tbody></table>';
valueElem.append(appendix);
}
我希望该函数能够更新选择,但会出错。
解决方案
实际上,解决问题的最简单方法是定义var thisInstance = this;
和使用withInstance.updateSelection
不与不同的范围冲突。
推荐阅读
- spring - Liquibase loadData 变更集忽略上下文
- python - Dash python 实时更新表
- reactjs - 如何在react中删除reducer中数组中的对象?
- php - php 服务器发送事件聊天应用程序的行为类似于轮询
- python - 我想从一个数据框中选择其值存在/不存在于另一个数据框中的所有记录。如何使用 pyspark 数据框做到这一点?
- javascript - 如何在 Angular HttpInterceptor 中使用异步 http 请求
- makefile - 关于makefile中的执行顺序
- python - Python - 将类变量指针分配给实例方法
- arrays - 如何将单元格概括为以行显示数据的列
- java - 在java 8中合并具有不同类型和长度的多个流