[英]Error when adding event listener to element when inside a class
當我運行代碼時,我收到一條錯誤消息: Uncaught TypeError: this.startButton.addEventlistener is not a function 我不知道如何修復。
我可以控制台記錄 class 內的按鈕,但無法添加奇怪的事件偵聽器
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 大寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.