基于时间消除警示框

7
我希望在一定时间后消失警示框。我听说在Javascript中这是不可能的,那么有没有其他方法可以实现?

你需要设计一个自定义的警告框,请阅读答案...它有效。 - user2273202
可能是重复的问题:Javascript关闭警告框 - user2273202
3个回答

4

尝试这个jsbin代码 - 为您定制的警告框

http://jsbin.com/ibUrIxu/1/edit

或者在您的.html文件上尝试这个

代码

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script>
function customAlert(msg,duration)
{
 var styler = document.createElement("div");
  styler.setAttribute("style","border: solid 5px Red;width:auto;height:auto;top:50%;left:40%;background-color:#444;color:Silver");
 styler.innerHTML = "<h1>"+msg+"</h1>";
 setTimeout(function()
 {
   styler.parentNode.removeChild(styler);
 },duration);
 document.body.appendChild(styler);
}
  function caller()
  {
    customAlert("This custom alert box will be closed in 2 seconds","2000");
  }
  </script>
  </head>
<body onload="caller()">

</body>
</html>

很方便!我根据自己的喜好微调了样式(codepen),但我相信它会被广泛使用。 :) - ashleedawg

1

演示 -- 这里有一个自定义的警示框和函数,是您需要的

function cancelDiv() {
        $("#div1").hide();
    }

    function ShowDiv() {
        $("#div1").css("backgroundColor", "white");
        $("#div1").show();
        setTimeout(function(){$("#div1").hide()}, 3000)
    }

    $(function () { 
        $("#div1").hide();
        $("#div1").draggable();            
    });

function ShowDiv()改为function showDiv()可能是一个好主意,函数名称中的大写字母通常用于标识对象创建。虽然这并不重要,但这是一个良好的格式和代码整理的建议。 - samrap
你不能说它是“Jugaad” @ Deepak,这在编程术语中是黑客或解决方法。 - Sudarshan Tanwar

0

您可以使用自定义警告消息来实现此操作。通知框架的一个示例是ToastR。您也可以创建自己的消息框架。 但是,您无法关闭常规的JavaScript警告框。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接