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 相关文章推荐
日期函数扩展类Ver0.1.1
Sep 07 Javascript
JS类库Bindows1.3中的内存释放方式分析
Mar 08 Javascript
juqery 学习之五 文档处理 插入
Feb 11 Javascript
网页中可关闭的漂浮窗口实现可自行调节
Aug 20 Javascript
jquery仅用6行代码实现滑动门效果
Sep 07 Javascript
JS全局变量和局部变量最新解析
Jun 24 Javascript
js鼠标移动时禁止选中文字
Feb 19 Javascript
Angular2 自定义validators的实现方法
Jul 05 Javascript
jQuery实现IE输入框完成placeholder标签功能的方法
Sep 20 jQuery
vue使用Element组件时v-for循环里的表单项验证方法
Jun 28 Javascript
Vue实现点击按钮复制文本内容的例子
Nov 09 Javascript
利用vue3+ts实现管理后台(增删改查)
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
php获取远程图片体积大小的实例
2013/11/12 PHP
PHP实现的oracle分页函数实例
2016/01/25 PHP
PHP去除字符串最后一个字符的三种方法实例
2017/03/01 PHP
XML的代替者----JSON
2007/07/21 Javascript
javascript 动态设置已知select的option的value值的代码
2009/12/16 Javascript
js关闭模态窗口刷新父页面或跳转页面
2012/12/13 Javascript
获取数组中最大最小值方法js代码(自写)
2013/08/12 Javascript
JSuggest自动匹配下拉框使用方法(示例代码)
2013/12/27 Javascript
使用CoffeeScrip优美方式编写javascript代码
2015/10/28 Javascript
jQuery解决浏览器兼容性问题案例分析
2016/04/15 Javascript
AngularJS中实现动画效果的方法
2016/07/28 Javascript
chrome浏览器如何断点调试异步加载的JS
2016/09/05 Javascript
IOS中safari下的select下拉菜单文字过长不换行的解决方法
2016/09/26 Javascript
JS基于正则表达式的替换操作(replace)用法示例
2017/04/28 Javascript
解决vue中使用Axios调用接口时出现的ie数据处理问题
2018/08/13 Javascript
微信小程序云开发修改云数据库中的数据方法
2019/05/18 Javascript
vue - props 声明数组和对象操作
2020/07/30 Javascript
Node.js path模块,获取文件后缀名操作
2020/11/07 Javascript
js删除指定位置超链接中含有百度与360的标题
2021/01/06 Javascript
[26:52]LGD vs EG 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
python利用elaphe制作二维条形码实现代码
2012/05/25 Python
Python中List.index()方法的使用教程
2015/05/20 Python
python遍历文件夹找出文件夹后缀为py的文件方法
2018/10/21 Python
django rest framework vue 实现用户登录详解
2019/07/29 Python
Python基于requests库爬取网站信息
2020/03/02 Python
美国一家著名的儿童鞋制造商:Stride Rite
2017/01/02 全球购物
怎样在 Applet 中建立自己的菜单(MenuBar/Menu)?
2012/06/20 面试题
大学生学业生涯规划
2014/01/05 职场文书
个人评价范文分享
2014/01/11 职场文书
表彰先进的通报
2014/01/31 职场文书
低碳环保标语
2014/06/12 职场文书
软环境建设心得体会
2014/09/09 职场文书
列车乘务员工作不细心检讨书
2014/10/07 职场文书
Go缓冲channel和非缓冲channel的区别说明
2021/04/25 Golang
浅谈react useEffect闭包的坑
2021/06/08 Javascript
分析Python list操作为什么会错误
2021/11/17 Python