Javascript 如何将setTimeout()添加到查询选择器
我有一个JS脚本Javascript 如何将setTimeout()添加到查询选择器,javascript,Javascript,我有一个JS脚本 <script> document.querySelectorAll('.stylebutton').forEach(function(e) { e.addEventListener('click', function() { this.style.backgroundColor = 'black'; }) }); </script> document.querySelectorAll('.stylebutton').forEac
<script>
document.querySelectorAll('.stylebutton').forEach(function(e) {
e.addEventListener('click', function() {
this.style.backgroundColor = 'black';
})
});
</script>
document.querySelectorAll('.stylebutton').forEach(函数(e){
e、 addEventListener('单击',函数()){
this.style.backgroundColor='black';
})
});
我想在跑步前等10秒钟。我试过了
<script>
document.querySelectorAll('.stylebutton').forEach(function(e) {
e.addEventListener('click', setTimeout(function() {
this.style.backgroundColor = 'black';
}),10000)
});
</script>
document.querySelectorAll('.stylebutton').forEach(函数(e){
e、 addEventListener('click',setTimeout(函数(){
this.style.backgroundColor='black';
}),10000)
});
但是我得到一个控制台错误
不能定义这个setTimeout
应该在回调内部调用
document.querySelectorAll('.stylebutton').forEach(function(e) {
e.addEventListener('click', function() {
setTimeout(()=>this.style.backgroundColor = 'black', 10000);
})
});
setTimeout
应在回调内部调用
document.querySelectorAll('.stylebutton').forEach(function(e) {
e.addEventListener('click', function() {
setTimeout(()=>this.style.backgroundColor = 'black', 10000);
})
});
在函数()中尝试=>setTiimeout….在运行附加事件侦听器的代码之前要等待10秒,或者在单击后运行每个事件侦听器的代码之前要等待10秒?在函数()中尝试=>setTiimeout….您想在运行附加事件侦听器的代码之前等待10秒,还是想在单击后在运行每个事件侦听器的代码之前等待10秒?太好了,谢谢!我不知道它必须在外面callback@hedgethenight很高兴能帮忙。太好了,谢谢!我不知道它必须在外面callback@hedgethenight很乐意帮忙。