工作需要,经理让做个对话提示框,三秒不点击自动关闭,用在web页面上。如果让我用c#写也许毫不费力,于是觉得并没什么困难,然而真做起来。。。
废话不说,问题如下:根据js运行机制,不可能将一个alert框强行关闭。前几天第一次做,查了好多资料,似乎找到了一个解决方法如test1.html。新建一个iframe,再借助于showModelessDialog来实现一个可关闭的alert。
于是乎问题似乎解决了,可是今天用的时候才发现,在IE7下,也许是IE7的安全机制的问题,这个方法并不能如愿运行,而且不能兼容firefox。可怜哪,在IE7下甚至都不能执行"javascript:alert()"语句。在网上查了以上午,也没找到有兼容性好的解决方案,于是决定自己重新做个兼容的,一直弄到晚上总算完工了如test2.html。希望对有此需求的人也能有所帮助...如需要,可下载压缩包
如果发现test2不兼容哪个浏览器的话,
如果有更好的思路,
请留言告诉我...
test1.html 经验证,适用于 IE6,不兼容IE7,firefox。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<input type="button" value="弹出alert框,一秒后自动关闭;" onClick="JavaScript:myAlert('不点击确定三秒后自动跳转')">
</body>
<script type="text/javascript">
/**
*@Description:弹出一个alert对话框,三秒钟内不点击确定,自动关闭alert框,该框最终模拟成非模态形式
* 经验证,适用于 IE6,不兼容IE7,firefox
*@param: showstr alert框里要显示的文本
*/
function myAlert(showstr)
{
var alertInfo = showstr; //传递alert框里要显示的信息
var timer = 3000; //设定停留的时间为3000ms,即3秒
//在页面中新建一个框架frame,以便在该frame中弹出一个非模态对话框
document.body.innerHTML += "<iframe style='display:none;' width='500' name='ifrtemp'></iframe>";
//在非模态对话框中弹出一个alert框,并立即关闭该非模态对话框
ifrtemp.showModelessDialog("javascript:alert('"+alertInfo+"');window.close();",
"",
"status:no;resizable:no;help:no;dialogHeight:530px;dialogWidth:40px;");
setTimeout("ifrtemp.location.reload();",timer); //停留一段时间后强行关闭alert窗口
}
</script>
</html>
test2.html 兼容 IE6 IE7 firefox
所用到几个图片放在压缩包里了
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>用div模拟alert对话框</title>
<!-- 为了增加适用性,所有的css样式都写在程序中,调用时只需将selfAlert类引入即可 -->
</head>
<body>
<br><br><center><h3 style="color:blue">用div模拟alert对话框,一段时间不点击,将自动关闭</h3></center>
</body>
<script type="text/javascript">
var s = new selfAlert("3秒钟不点击将自动关闭",3000);
/**
* Descrioption: 模拟alert对话框,一定时间内不点击则自动关闭
* param msgstr: 模拟对话框要显示的字符串
* timer: 自动关闭时间
* ps: 适用于 IE6 IE7 firefox
*/
function selfAlert(msgstr,timer){
//该值可以作为返回值,初始化时为 0 ,点击确定后变为 1 ,点击关闭后变为 2 ,自动关闭 3
var alertValue = 0;
//确定遮罩层的高度,宽度
var h = screen.availHeight;
var w = screen.availWidth;
//创建遮罩层,它的主要作用就是使网页中的其他元素不可用。
var dv = document.createElement("div");
dv.setAttribute('id','bg');
//设置样式
dv.style.height = h + "px";
dv.style.width = w + "px";
dv.style.zIndex = "1111";
dv.style.top = 0;
dv.style.left = 0;
//如果不想遮罩,可以去掉这两句
dv.style.background = "#fff";
dv.style.filter = "alpha(opacity=0)";
//设为绝对定位很重要
dv.style.position = "absolute";
//将该元素添加至body中
document.body.appendChild(dv);
//创建提示对话框面板
var dvMsg = document.createElement("div");
dvMsg.style.position = "absolute";
dvMsg.setAttribute('id','msg');
dvMsg.style.width = "280px";
dvMsg.style.height = "100px";
dvMsg.style.top="30%";
dvMsg.style.left="40%";
dvMsg.style.background = "white";
dvMsg.style.zIndex = "1112";
//可以继续采用如上形式创建模拟对话框表格,这里为了方便采用html形式
strHtml = "<table width='280' height='25' border='0' cellspacing='0' cellpadding='0' align='center'>"
strHtml += " <tr height='25' style='line-height:25px;'>"
strHtml += " <td width='250' title='移动' style='cursor:move;background:#CFD7EC url(title_bg_left.gif) no-repeat top left;' onmousedown='oMove(parentNode.parentNode.parentNode.parentNode);'>"
strHtml += " <font style='font-size:12px;font-weight:bold;color:#000;margin-left:10px;'>消息提示框</font></td>"
strHtml += " <td width='30' style='background:#CFD7EC url(title_bg_right.gif) no-repeat right top;'>"
strHtml += " <img src='close.gif' style='margin-right:3px;cursor:hand;' onclick='imgClose();'><td></tr>"
strHtml += "</table>"
strHtml += "<table width='280' height='145' border='0' cellspacing='0' cellpadding='0' align='center' style='border:1px solid #343434'>"
strHtml += " <tr height='113' bgcolor='#F4F4F4'><td width='' style='padding-left:10;'><img src='info.gif'></td>"
strHtml += " <td width='200' align='left'>" + msgstr + "</td></tr>"
strHtml += " <tr height='27'><td colspan='2' style='background:#F4F4F4;padding-top:0px;' valign='top' align='center'>"
strHtml += " <input type='button' value='确 定' style='width:70;' onclick='btnclick()'></td></tr>"
strHtml += "</table>"
dvMsg.innerHTML = strHtml;
document.body.appendChild(dvMsg);
//点击关闭按钮
imgClose = function (){
alertValue = 2; // 2 代表点击了关闭按钮
document.body.removeChild(dv);
document.body.removeChild(dvMsg);
}
//点击确定按钮
btnclick = function (){
alertValue = 1; // 1 代表点击了确定按钮
document.body.removeChild(dv);
document.body.removeChild(dvMsg);
}
remove = function ()
{
//timer时间过后如果仍未点击,则自动关闭selfAlert框
if(alertValue==0){
document.body.removeChild(dv);
document.body.removeChild(dvMsg);
}
}
//timer秒后自动关闭selfAlert(提示框)
setTimeout("remove()",timer);
//实现鼠标拖动对话框
oMove = function(obj) {
var otop,oleft;
otop = event.y - obj.offsetTop;
oleft = event.x - obj.offsetLeft;
obj.setCapture();
obj.onmousemove = function()
{
obj.style.left = event.x - oleft;
obj.style.top = event.y - otop;
}
obj.onmouseup = function()
{
obj.onmousemove = null;
obj.style.filter = null;
obj.releaseCapture();
}
}
}
</script>
</html>
分享到:
- 2009-11-26 21:36
- 浏览 11299
- 评论(33)
- 论坛回复 / 浏览 (32 / 23280)
- 查看更多
相关推荐
因此简单用div写了一个alert提示框,并自动关闭。 效果图 css样式 /*弹出消息对话框样式*/ .show_alert_box{ width:100%; height:100%; position:fixed; top:0px; left:0px; background-color:rgba(0,0,0,0.6); ...
Div模拟对话框 <br>Div模拟对话框
纯js(javascript) 弹出层 对话框 弹出层,可以有半透明遮挡层。 div 模拟系统对话框,Alert Confirm Prompt
纯CSS实现div弹出对话框 可以应用于C#,.net 里面。 页面比较好看。
js实现div模拟模态对话框展现URL内容
十年积累,div+css 完全兼容 ie6 ie7 IE8 IE9 和firefox方法
使用DIV模拟对话框alert confirm
用div模拟163对话框,可拖拉,可以根据自己的需求更改源码 大家打开的如果发生脚本错误,出现空白页,没关系那是因为乱码,请加上 ;charset=UTF-8" />
本文实例讲述了js实现div模拟模态对话框展现URL内容。分享给大家供大家参考,具体如下: [removed] function sAlert(str){ var msgw,msgh,bordercolor; msgw=800;//提示窗口的宽度 msgh=600;//提示窗口...
div弹出对话框div弹出对话框div弹出对话框div弹出对话框div弹出对话框div弹出对话框div弹出对话框div弹出对话框
明锋工作室整理常用资料。本次资料是一个自动居中插件,只要设定div长度和高度,其中的图片会自动铺满容器并且自动居中。...并且该自动居中插件支持IE6、IE7、IE8、IE9、IE10、CHROME、FIREFOX、OPERA。
弹出窗体 PopupDIV 浮出对话框 支持IE6,Firefox,IE7效果不好
在windowsXP sp2下的 IE8、firefox3.5、opera10、傲游、TT浏览器、搜狗浏览器、360绿色浏览器下调试通过。 本div+js实现的弹出对话框,是由本人用网上两位不知名的大虾的代码合并、修改完善而得,已经经过充分的调试...
用javascript css div实现弹出模态对话框,盖住下面的部分,上面的透明度改变
(十年积累)div+css_完全兼容ie6_ie7_IE8_IE9_和firefox方法.doc\(十年积累)div+css_完全兼容ie6_ie7_IE8_IE9_和firefox方法.doc
自己改写的js弹出div层模拟alert 可以自定义样式 在iframe框架中使用 避免了覆盖层只覆盖子页面的问题
js+div 超酷模态对话框[带提示声音]
Javascript实现div对话框效果