我希望在一定时间后消失警示框。我听说在Javascript中这是不可能的,那么有没有其他方法可以实现?
尝试这个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>
演示 -- 这里有一个自定义的警示框和函数,是您需要的
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您可以使用自定义警告消息来实现此操作。通知框架的一个示例是ToastR。您也可以创建自己的消息框架。 但是,您无法关闭常规的JavaScript警告框。