我希望点击按钮后几秒钟内无法点击,我该怎么办?我想为所有按钮编写代码,而不仅仅是一个按钮。所以,我需要这样的东西:
$(document).ready(function() {
$('input[type="button"]').click(function() {
$(this).css("hidden: true");
delay(3);
$(this).css("hidden: normal");
})
})
这只是一个伪代码,你能帮我一下吗?
最佳答案
使用 jQuery...
$(document).ready(function() {
$('input[type="button"]').click(function() {
var input = this;
input.disabled = true;
setTimeout(function() {
input.disabled = false;
}, 3000);
});
});
jsFiddle .
没有 jQuery...
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('input[type="button"]')
.addEventListener('click', (e) => {
let input = e.target;
input.disabled = true;
setTimeout(function() {
input.disabled = false;
}, 3000);
});
});
或者捕获对持久祖先元素的点击并检查它们是否是 input[type="button"]
类型。这可以防止为每个元素附加多个事件。
关于jquery - 如何让按钮点击后不可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5830781/