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 相关文章推荐
跟着JQuery API学Jquery 之三 筛选
Apr 09 Javascript
通过javascript把图片转化为字符画
Oct 24 Javascript
jquery.cookie() 方法的使用(读取、写入、删除)
Dec 05 Javascript
浅谈轻量级js模板引擎simplite
Feb 13 Javascript
javascript中Array()数组函数详解
Aug 23 Javascript
基于JavaScript实现全屏透明遮罩div层锁屏效果
Jan 26 Javascript
jQuery Easyui Datagrid实现单行的上移下移及保存移动的结果
Aug 15 Javascript
Bootstrap CSS布局之表格
Dec 17 Javascript
关于vuex的学习实践笔记
Apr 05 Javascript
JS实现的简单折叠展开动画效果示例
Apr 28 Javascript
three.js利用gpu选取物体并计算交点位置的方法示例
Nov 25 Javascript
js实现百度淘宝搜索功能
Feb 17 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
模拟OICQ的实现思路和核心程序(一)
2006/10/09 PHP
php正则表达匹配中文问题分析小结
2012/03/25 PHP
PHP实现支持SSL连接的SMTP邮件发送类
2015/03/05 PHP
PHP加密解密类实例代码
2016/07/20 PHP
thinkPHP+phpexcel实现excel报表输出功能示例
2017/06/06 PHP
基于jQuery的烟花效果(运动相关)点击屏幕出烟花
2012/06/14 Javascript
jQuery插件实现静态HTML验证码校验
2015/11/06 Javascript
React Native如何消除启动时白屏的方法
2017/08/08 Javascript
JS+jQuery实现注册信息的验证功能
2017/09/26 jQuery
Angular2 父子组件通信方式的示例
2018/01/29 Javascript
不使用JavaScript实现菜单的打开和关闭效果demo
2018/05/01 Javascript
vue自定义移动端touch事件之点击、滑动、长按事件
2018/07/10 Javascript
ES6中let 和 const 的新特性
2018/09/03 Javascript
node.js监听文件变化的实现方法
2019/04/17 Javascript
JS开发 富文本编辑器TinyMCE详解
2019/07/19 Javascript
浅析TypeScript 命名空间
2020/03/19 Javascript
Vue 中使用lodash对事件进行防抖和节流操作
2020/07/26 Javascript
vue抽出组件并传值实例
2020/07/31 Javascript
[01:16:13]DOTA2-DPC中国联赛 正赛 SAG vs Dragon BO3 第一场 2月22日
2021/03/11 DOTA
Python多线程实现同步的四种方式
2017/05/02 Python
python中matplotlib实现最小二乘法拟合的过程详解
2017/07/11 Python
Python排序搜索基本算法之希尔排序实例分析
2017/12/09 Python
numpy中实现二维数组按照某列、某行排序的方法
2018/04/04 Python
python下解压缩zip文件并删除文件的实例
2018/04/24 Python
python中for循环输出列表索引与对应的值方法
2018/11/07 Python
tensorflow使用range_input_producer多线程读取数据实例
2020/01/20 Python
Python生成器实现简单&quot;生产者消费者&quot;模型代码实例
2020/03/27 Python
澳大利亚领先的时尚内衣零售商:Bras N Things
2020/07/28 全球购物
卖车协议书
2014/04/21 职场文书
企业文化口号
2014/06/12 职场文书
2014镇副书记群众路线专题民主生活会思想汇报
2014/09/23 职场文书
井冈山红色之旅心得体会
2014/10/07 职场文书
起诉离婚协议书样本
2014/11/25 职场文书
幼儿园感恩节活动总结
2015/03/24 职场文书
经济纠纷起诉状
2015/05/20 职场文书
党员干部学习心得体会
2016/01/23 职场文书