有没有办法模拟确认框上的回车键?
confirm("按下一个按钮!");
创建的确认框类型。
有没有办法模拟确认框上的回车键?
confirm("按下一个按钮!");
创建的确认框类型。
如果是在网页中运行的JavaScript,其实不行。
标准浏览器的confirm
对话框是模态和阻塞的。当它弹出时,页面上绝对不会运行任何JS代码。
你最接近的方法就是完全重写这个函数。
function yes() {
document.body.appendChild(document.createTextNode("You said yes! "));
}
function sure() {
if (confirm("Are you sure?")) {
yes();
}
}
document.querySelector("button").addEventListener("click", sure);
window.confirm = function myConfirm() {
return true;
}
<button>Confirm</button>