只有在甜蜜警报关闭后,我才能将注意力集中在页面中的文本框上。这是我正在使用的代码
swal({
title:'Student Already Logged In!',
text: 'Student already logged in.',
type: 'warning',
timer: 2500,
showConfirmButton: false
});
最佳答案
如果您使用的是 SweetAlert2,则需要使用 myElement.focus();
添加 promise ,如 documentation 中所述。 .
例如你的文本框有id='my-input'
你需要的JS代码是:
(function() {
swal({
title: 'Student Already Logged In!',
text: 'Student already logged in.',
type: 'warning',
timer: 2500,
showConfirmButton: false
}).then(function() {
document.getElementById('my-input').focus();
});
})()
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.0/sweetalert.min.js"></script>
<input type="text" id="my-input">
或者,如果您使用的是 SweetAlert (v1.x),则需要将回调作为 swal()
的第二个参数传递。
swal({
title: 'Student Already Logged In!',
text: 'Student already logged in.',
type: 'warning',
timer: 2500,
showConfirmButton: false
}, function() {
document.getElementById('my-input').focus();
});
关于javascript - 仅在甜蜜警报关闭后自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886834/