javascript - 在类中向元素添加事件侦听器时出错
问题描述
当我运行代码时,我收到一条错误消息:未捕获的 TypeError:this.startButton.addEventlistener 不是我不知道如何修复的函数。
我可以 console.log 类内的按钮,但不能添加奇怪的事件监听器
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<input id="duration" />
<button id="start">Start</button>
<button id="pause">Pause</button>
<script src="index.js"></script>
</body>
</html>
JS:
class Timer{
constructor(durationInput, startButton, pauseButton){
this.durationInput = durationInput;
this.startButton = startButton;
this.pauseButton = pauseButton;
this.startButton.addEventlistener('click',this.start)
}
start(){
console.log("time to start the timer")
}
}
const durationInput = document.querySelector('duration');
const startButton = document.querySelector('#start');
const pauseButton = document.querySelector('#pause');
const timer = new Timer(durationInput, startButton, pauseButton)
解决方案
你只是有一个错字,它是this.startButton.addEventListener('click',this.start)
L 大写。
推荐阅读
- vba - CATVBA InputObjectTypes 列表?
- xpages - 为空时隐藏 xp:mesages 框
- c# - 在 Core 中配置 URL 重写以获取 swagger、API 和 Angular 重新路由
- javascript - 使用 JavaScript 中来自多个外部 js 文件的不同数组连接相同的变量
- javascript - html5、css3、javascript、jquery
- solr - Solr - 改变分数的计算方式?(总和而不是最大值)
- microservices - 更新/添加为单独的服务和获取为单独的服务
- spring - 带有 Spring 配置文件和基于注释的配置的 WebApplicationInitializer
- android - 在后台使用麦克风
- pandas - 在 pandas DataFrame 上创建新列,其中条目是从另一列中随机选择的条目