jQuery 自动关闭警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19020830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autoclose alert
提问by Okky
Is there any way to close a javascript alert()
automatically?
有没有办法alert()
自动关闭javascript ?
I have an alert
我有一个警报
alert("Error found");
I want to close it after a few second. Is that possible or shall I go for jQuery dialogue
我想在几秒钟后关闭它。这是可能的还是我应该去 jQuery 对话
回答by NaYaN
This functionality is not possible with an alert. However, you could use a div
警报无法实现此功能。但是,您可以使用 div
function tempAlert(msg,duration)
{
var el = document.createElement("div");
el.setAttribute("style","position:absolute;top:40%;left:20%;background-color:white;");
el.innerHTML = msg;
setTimeout(function(){
el.parentNode.removeChild(el);
},duration);
document.body.appendChild(el);
}
Use this like this:
像这样使用它:
tempAlert("close",1000);
回答by woold Lucky
Basically you can imitate an alert box with a popup window and close it whenever you want, call the following function:
基本上你可以模仿一个带有弹出窗口的警告框并在需要时关闭它,调用以下函数:
function myAlert(msg,title,width,height,timeout) {
var myWindow = window.open("", "",`width=${width},height=${height},left=${(window.outerWidth/2-width/2)},top=0`); //open a new popup at the top height and middle width of the current window
myWindow.document.write(`<center id="msg">`+msg+`</center>`); //write the message you want to display
myWindow.document.title = title; //write the title of the alert box
setTimeout(function(){
myWindow.close(); //close the popup
},timeout||3000) //in 3 secondes (3000 milliseconds)
}
by the way if you want to close the alert on call, just use define the variable "myWindow" earlier (out of myAlert() function) as a global variable and then call myWindow.close();
once you already called myAlert() and remove
顺便说一下,如果您想在调用时关闭警报,只需使用之前(在 myAlert() 函数之外)将变量“myWindow”定义为全局变量,然后myWindow.close();
在您已经调用 myAlert() 并删除后调用
setTimeout(function(){
myWindow.close(); //close the popup
},timeout||3000) //in 3 secondes (3000 milliseconds)
from the function myAlert()
来自函数 myAlert()
回答by HybrisHelp
You can't close alert any how .
您无法以任何方式关闭警报。
But you can use div To show your alert MSG.
但是您可以使用 div 来显示您的警报 MSG。
function Mymsg(msg,duration)
{
var alt = document.createElement("div");
alt.setAttribute("style","position:absolute;top:50%;left:50%;background-color:white;");
alt.innerHTML = msg;
setTimeout(function(){
alt.parentNode.removeChild(alt);
},duration);
document.body.appendChild(alt);
}
You can use as :
您可以用作:
Mymsg('close',2000)
回答by Ken H
I updated the style settings, so it shows like a splash screen in the center of your page, and centers the text inside it.
我更新了样式设置,因此它在页面中央显示为启动画面,并将其中的文本居中。
Call it like this:
像这样调用它:
alertTimeout("System Message<br>This is a test message<br>This alert will auto-close",5000)
The function:
功能:
function alertTimeout(mymsg,mymsecs)
{
var myelement = document.createElement("div");
myelement.setAttribute("style","background-color: grey;color:black; width: 450px;height: 200px;position: absolute;top:0;bottom:0;left:0;right:0;margin:auto;border: 4px solid black;font-family:arial;font-size:25px;font-weight:bold;display: flex; align-items: center; justify-content: center; text-align: center;");
myelement.innerHTML = mymsg;
setTimeout(function(){
myelement.parentNode.removeChild(myelement);
},mymsecs);
document.body.appendChild(myelement);
}