Js 订制自己的AlertBox(信息提示框)


Posted in Javascript onJanuary 09, 2009

本文制作一个用户自定义的AlertBox,效果如图:
Js 订制自己的AlertBox(信息提示框)
js文件中插入如下代码:

// JScript 文件 
// constants to define the title of the alert and button text. 
var ALERT_TITLE = "Oops!"; 
var ALERT_BUTTON_TEXT = "Close"; 
// over-ride the alert method only if this a newer browser. 
// Older browser will see standard alerts 
if(document.getElementById) { 
window.alert = function(txt) { 
createCustomAlert(txt); 
} 
} 
function createCustomAlert(txt) { 
// shortcut reference to the document object 
d = document; 
// if the modalContainer object already exists in the DOM, bail out. 
if(d.getElementById("modalContainer")) return; 
// create the modalContainer div as a child of the BODY element 
mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div")); 
mObj.id = "modalContainer"; 
// make sure its as tall as it needs to be to overlay all the content on the page 
mObj.style.height = document.documentElement.scrollHeight + "px"; 
// create the DIV that will be the alert 
alertObj = mObj.appendChild(d.createElement("div")); 
alertObj.id = "alertBox"; 
// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert 
if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px"; 
// center the alert box 
alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px"; 
// create an H1 element as the title bar 
h1 = alertObj.appendChild(d.createElement("h1")); 
h1.appendChild(d.createTextNode(ALERT_TITLE)); 
// create a paragraph element to contain the txt argument 
msg = alertObj.appendChild(d.createElement("p")); 
msg.innerHTML = txt; 
// create an anchor element to use as the confirmation button. 
btn = alertObj.appendChild(d.createElement("a")); 
btn.id = "closeBtn"; 
btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT)); 
btn.href = "#"; 
// set up the onclick event to remove the alert when the anchor is clicked 
btn.onclick = function() { removeCustomAlert();return false; } 
// removes the custom alert from the DOM function removeCustomAlert() { 
// document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer")); 
} 
// removes the custom alert from the DOM 
function removeCustomAlert() 
{ 
document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer")); 
}

将如下代码粘贴到你的HTML你的HTML的HEAD部分。 
<script type="text/javascript" src="include/customAlertBox.js"></script> 
<!-- Paste this code into your external style sheet or the 
CSS section of your HTML document --> 
<style type="text/css"> 
#modalContainer { 
background-color:transparent; 
position:absolute; 
width:100%; 
height:100%; 
top:0px; 
left:0px; 
z-index:10000; 
} 
#alertBox { 
position:relative; 
width:300px; 
min-height:100px; 
margin-top:50px; 
border:2px solid #000; 
background-color:#F2F5F6; 
background-image:url(alert.png); 
background-repeat:no-repeat; 
background-position:20px 30px; 
} 
#modalContainer > #alertBox { 
position:fixed; 
} 
#alertBox h1 { 
margin:0; 
font:bold 0.9em verdana,arial; 
background-color:#78919B; 
color:#FFF; 
border-bottom:1px solid #000; 
padding:2px 0 2px 5px; 
} 
#alertBox p { 
font:0.7em verdana,arial; 
height:50px; 
padding-left:5px; 
margin-left:55px; 
} 
#alertBox #closeBtn { 
display:block; 
position:relative; 
margin:5px auto; 
padding:3px; 
border:1px solid #000; 
width:70px; 
font:0.7em verdana,arial; 
text-transform:uppercase; 
text-align:center; 
color:#FFF; 
background-color:#78919B; 
text-decoration:none; 
} 
</style>

在你的HTML文档的Body部分插入如下代码:
<input type="button" value = "Test the alert" onclick="alert('This is a custom alert dialog that was created by overriding the window.alert method.');">

Javascript 相关文章推荐
FireFox JavaScript全局Event对象
Jun 14 Javascript
javascript 面向对象 function类
May 13 Javascript
JavaScript 开发规范要求(图文并茂)
Jun 11 Javascript
js防止表单重复提交实现代码
Sep 05 Javascript
jquery.validate的使用说明介绍
Nov 12 Javascript
js采用map取到id集合组并且实现点击一行选中一行
Dec 16 Javascript
关闭浏览器窗口弹出提示框并且可以控制其失效
Apr 15 Javascript
JavaScript的jQuery库中function的存在和参数问题
Aug 13 Javascript
使用BootStrapValidator完成前端输入验证
Sep 28 Javascript
微信小程序画布圆形进度条显示效果
Nov 17 Javascript
vue响应式系统之observe、watcher、dep的源码解析
Apr 09 Javascript
茶余饭后聊聊Vue3.0响应式数据那些事儿
Oct 30 Javascript
通用JS事件写法实现代码
Jan 07 #Javascript
javascript 表单的友好用户体现
Jan 07 #Javascript
JavaScript Prototype对象
Jan 07 #Javascript
开发跨浏览器javascript常见注意事项
Jan 01 #Javascript
用于判断用户注册时,密码强度的JS代码
Jan 01 #Javascript
很全的显示阴历(农历)日期的js代码
Jan 01 #Javascript
js继承 Base类的源码解析
Dec 30 #Javascript
You might like
动态网站web开发 PHP、ASP还是ASP.NET
2006/10/09 PHP
PHP及Zend Engine的线程安全模型分析
2011/11/10 PHP
PHP+Ajax实现的无刷新分页功能详解【附demo源码下载】
2017/07/03 PHP
Aster vs Newbee BO5 第二场2.19
2021/03/10 DOTA
JavaScript高级程序设计 事件学习笔记
2011/09/10 Javascript
javascript 正则表达式相关应介绍
2012/11/27 Javascript
关于onchange事件在IE和FF下的表现及解决方法
2014/03/08 Javascript
Javascript中使用A标签获取当前目录的绝对路径方法
2015/03/02 Javascript
Javascript中的Callback方法浅析
2015/03/15 Javascript
学习使用grunt来打包JavaScript和CSS程序的教程
2016/01/04 Javascript
jQuery绑定事件监听bind和移除事件监听unbind用法实例详解
2016/01/19 Javascript
简单理解JavaScript中的封装与继承特性
2016/03/19 Javascript
在html中引入外部js文件,并调用带参函数的方法
2016/10/31 Javascript
原生js获取浏览器窗口及元素宽高常用方法集合
2017/01/18 Javascript
Web开发中客户端的跳转与服务器端的跳转的区别
2017/03/05 Javascript
关于JavaScript中的this指向问题总结篇
2017/07/23 Javascript
原生JS封装_new函数实现new关键字的功能
2018/08/12 Javascript
vue实现一个炫酷的日历组件
2018/10/08 Javascript
JQuery获得内容和属性方法解析
2020/05/30 jQuery
[02:23]DOTA2英雄基础教程 幻影长矛手
2013/12/09 DOTA
python strip()函数 介绍
2013/05/24 Python
Python中的面向对象编程详解(上)
2015/04/13 Python
Python实现基于权重的随机数2种方法
2015/04/28 Python
python使用matplotlib绘图时图例显示问题的解决
2017/04/27 Python
Python实现PS滤镜的万花筒效果示例
2018/01/23 Python
对Python 3.5拼接列表的新语法详解
2018/11/08 Python
深入学习python多线程与GIL
2019/08/26 Python
Python三元运算与lambda表达式实例解析
2019/11/30 Python
浅谈keras中自定义二分类任务评价指标metrics的方法以及代码
2020/06/11 Python
详解css3 mask遮罩实现一些特效
2018/10/24 HTML / CSS
CSS实现半透明边框与多重边框的场景分析
2019/11/13 HTML / CSS
程序设计HTML5 Canvas API
2013/04/08 HTML / CSS
html5 worker 实例(一) 为什么测试不到效果
2013/06/24 HTML / CSS
初中生散播谣言检讨书
2014/11/17 职场文书
2014年企业党建工作总结
2014/12/18 职场文书
2015年大学班长个人工作总结
2015/04/24 职场文书