我正在构建一个 vue 组件,我想通过在修改 innerHTML 时调用 onClick 属性来调用在 Vue 方法中定义的函数。但是,它给了我错误,“未定义 showModal”,如下所示。
这是我的 showModal 函数,我只是想控制一些数据以确保调用该函数:
methods: {
showModal: function() {
console.log("function is called")
//this.$refs.myModalRef.show();
//this.account=account;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试通过 onclick 函数在 js 中调用该函数的地方:
var inputText = document.getElementById("fileContents");
var innerHTML = inputText.innerHTML;
for(var i=0;i<this.violations.length;i++){
var index=innerHTML.indexOf(this.violations[i]);
if(index>0) {
innerHTML = innerHTML.substring(0, index) +"<a href='#' onclick='showModal'>"+ "<span style=\"background-color: yellow\">" + innerHTML.substring(index, index + this.violations[i].length) + "</span>" + "</a>" + innerHTML.substring(index + this.violations[i].length);
inputText.innerHTML = innerHTML;
}
}
Run Code Online (Sandbox Code Playgroud)
错误: (index):1 Uncaught ReferenceError: showModal 未在 HTMLAnchorElement.onclick ((index):1) …