gpt4 book ai didi

html - 我想使用 DOM 但现在我有错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:37 25 4
gpt4 key购买 nike

我想在放置按钮时获取信息,为此我使用了 DOM,但是我有错误,我该如何解决这个问题?

我试图确认名称是否正确,并搜索了 DOM,但我无法弄清楚。

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="uft-8">
<title>Pomodoro Timer</title>
</head>
<body>
<p class="menu">menu</p>
<p class="timer">0.00</p>
<button data-option='start' class="timer_button" >start</button>
<button data-option='stop' class="timer_button">stop</button>
<button data-option='reset' class="timer_button">reset</button>
<script>
const buttons = document.querySelectorAll('[data-option]');

function test(e){
console.log(e);
}
buttons.forEach(()=>buttons.addEventListener('click',test));


</script>
</body>

我希望在按下按钮时在控制台中看到结果。

最佳答案

buttons.forEach((button)=>button.addEventListener('click',test))

buttons 是一个列表。当您使用 forEach 对其进行迭代时,您可以获得每个按钮作为该方法的第一个参数。这就是您必须附加事件处理程序的地方。

关于html - 我想使用 DOM 但现在我有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com