网页总自动关闭:自动关闭警报(alert auto close javascript)

有没有办法自动关闭 javascriptalert()

我有一个警报

alert("Error found");

我想在几秒钟后关闭它。这是可能的还是我应该去 jQuery 对话

15
jsFiddle Demo

此功能不可能与警报。但是,您可以使用 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);
}

使用这样的:

tempAlert("close",1000);
3

你不能以任式关闭警报。

但是你可以使用 div 来显示你的警报味精。

  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);
}

您可以使用:

Mymsg('close',2000)
3
jsFiddle Demo

基本上你可以模仿一个弹出窗口的警报框,并随时关闭它,调用以下函数:

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)
}

顺便说一句,如果你想在通话中关闭警报,只需使用定义变量“myWindow”(从 myAlert()函数)作为全局变量,然后调用myWindow.close();一旦你已经调用 myAlert()并删除

setTimeout(function(){
    myWindow.close(); //close the popup
},timeout||3000) //in 3 secondes (3000 milliseconds)

从函数 myAlert ()

2

我更新了样式设置,因此它在页面中心显示为闪屏,并将文本居中。

像这样调用它:

alertTimeout("System Message<br>This is a test message<br>This alert will auto-close",5000)

功能:

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);
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(777)
服务器创建环境:使用无服务器框架为 AWS设置无服务器本地环境
上一篇
Un def:无法使用伪造查看器编译反应应用程序-无法编译-Autodesk未定义un-def
下一篇

相关推荐

  • javascript开发桌面应用从入门到精通

    JavaScript是一种跨平台的脚本语言,可以用来开发桌面应用。它可以在浏览器中运行,也可以在服务器端运行,甚至可以用来开发桌面应用。…

    2023-03-12 12:03:42
    0 67 95
  • javascript可以跨平台吗优势与挑战。

    答:是的,javascript可以跨平台。它可以在多种操作系统和浏览器上运行,如Windows、Mac OS、Linux和Chrome、Firefox等。下面是一个简单的javascript代码示例,用于显示“Hello World”:…

    2023-04-24 12:52:30
    0 96 11
  • javascript权威:JavaScript的基本概念

    JavaScript 权威指的是 JavaScript 的标准,也就是 ECMAScript(ECMA-262)。它是由 ECMA 国际组织制定的一种语言标准,它定义了 JavaScript 语言的语法和行为,以及实现 JavaScript 的规范。…

    2023-02-08 09:10:25
    0 13 32
  • javascript对象:利用JavaScript对象实现复杂数据结构

    JavaScript对象是一种无序的集合数据类型,用于存储和检索信息。它由一系列键/值对组成,其中键是一个字符串,而值可以是任何有效的JavaScript数据类型,包括另一个对象。…

    2023-03-08 12:09:06
    0 59 59
  • javascript作用域理解全局作用域、函数作用域和块级作用域

    示例示例作用域指的是代码中定义变量和函数的范围,它决定了在何处可以访问到这些变量和函数。中有两种作用域:全局作用域和局部作用域。…

    2023-01-29 01:03:11
    0 18 79
  • javascript作用:如何利用JavaScript来创建强大的Web应用

    JavaScript是一种轻量级的编程语言,它可以用于创建动态的、交互式的网页和应用程序。它是一种客户端脚本语言,这意味着它可以在用户的浏览器中运行,而无需向服务器发送请求。…

    2023-05-02 11:23:28
    0 89 74
  • javascript的输出语句:使用JavaScript输出结果

    JavaScript的输出语句是指在网页中显示文本或其他内容的语句,常用的输出语句有document.write()、window.alert()和console.log()。…

    2023-05-14 13:12:22
    0 78 77
  • javascript返回上一页:返回上一页

    返回上一页可以使用 window.history.back() 方法。代码示例:…

    2023-08-06 05:44:54
    0 91 35

发表评论

登录 后才能评论

评论列表(61条)