我使用JS的新类语法。当我按下按钮时,我想调用一个类的方法。为了实现这一点,我将此方法设置为静态。类未定义onClick事件
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没有定义。我真的不明白,那里有什么问题。
我的项目文件夹中的图片:
也许这会有所帮助。
NoteController.js是否在您的HTML页面是相同的文件夹?你得到什么确切的错误?我敢打赌我的钱没有被加载到HTML页面上。 – ProgrammerV5
我刚刚更新了我的照片:) – peterHasemann
添加了一个响应和一个工作示例。没有像在你的结构中那样在文件中分开,但我希望它有助于理解你的工作代码中缺少什么。 – ProgrammerV5