未定义类的onClick事件 [英] Class is not defined onClick event
本文介绍了未定义类的onClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对JS使用了新的类语法.当我按下一个按钮时,我想从一个类中调用一个方法.为此,我将此方法设置为静态.
I use the new class syntax for JS. When I press a button, I want to call a method from a class. To archieve this, I set this method static.
class NoteController { // The controller class
constructor() { // Initialization
// ...
}
static CreateNote() { // The method to call - static
// ....
}
}
<!DOCTYPE html>
<html>
<head>
// ....
<script src="NoteController.js"></script> // Link the js file to the html file
</head>
<body>
// ....
<button type="button" id="btnCreateNote" onclick="NoteController.CreateNote()">Create</button> // call the method
</body>
</html>
当我单击按钮时,它说未定义NoteController.我真的不明白,那是怎么回事.
When I click the button, it says, NoteController is not defined. I really do not get, what is wrong there.
我的项目文件夹的图片:
A picture of my project folder:
也许这会有所帮助.
推荐答案
使其变为静态的校正:
<script>
class NoteController { // The controller class
constructor() { // Initialization
// ...
alert('constructor called');
}
static CreateNote() { // The method to call - static
// ....
alert('create note');
}
}
//var nc=new NoteController();
//console.log(nc);
</script>
<button type="button" id="btnCreateNote" onclick="NoteController.CreateNote()">Create</button> // call the method
这是您的代码的有效示例:
This is the working example of your code:
<script>
class NoteController { // The controller class
constructor() { // Initialization
// ...
alert('constructor called');
}
CreateNote() { // The method to call - static
// ....
alert('create note');
}
}
var nc=new NoteController();
console.log(nc);
</script>
<button type="button" id="btnCreateNote" onclick="nc.CreateNote()">Create</button> // call the method
工作 JSFiddle
这篇关于未定义类的onClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文