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 相关文章推荐
简略说明Javascript中的= =(等于)与= = =(全等于)区别
Apr 16 Javascript
js中浮点型运算BUG的解决方法说明
Jan 06 Javascript
微信JS接口大全
Aug 25 Javascript
分享19个JavaScript 有用的简写写法
Jul 07 Javascript
jquery实现倒计时小应用
Sep 19 jQuery
实例分析js事件循环机制
Dec 13 Javascript
基于node.js实现微信支付退款功能
Dec 19 Javascript
Vue.js组件间通信方式总结【推荐】
Nov 23 Javascript
通过实例了解js函数中参数的传递
Jun 15 Javascript
vue项目引入ts步骤(小结)
Oct 31 Javascript
Vue按时间段查询数据组件使用详解
Aug 21 Javascript
jQuery实现购物车全功能
Jan 11 jQuery
通用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
PHP5.2中date()函数显示时间与北京时间相差8小时的解决办法
2009/05/28 PHP
vs中通过剪切板循环来循环粘贴不同内容
2011/04/30 PHP
PHP中对数组的一些常用的增、删、插操作函数总结
2015/11/27 PHP
php图片上传类 附调用方法
2016/05/15 PHP
基于jquery的3d效果实现代码
2011/03/23 Javascript
js 将json字符串转换为json对象的方法解析
2013/11/13 Javascript
仿JQuery输写高效JSLite代码的一些技巧
2015/01/13 Javascript
jquery获取当前日期的方法
2015/01/14 Javascript
高性能JavaScript模板引擎实现原理详解
2015/02/05 Javascript
jQuery+AJAX实现无刷新下拉加载更多
2015/07/03 Javascript
Uploadify上传文件方法
2016/03/16 Javascript
jQuery 判断是否包含在数组中Array[]的方法
2016/08/03 Javascript
AngularJS 自定义指令详解及示例代码
2016/08/17 Javascript
Vue实现路由跳转和嵌套
2017/06/20 Javascript
jQuery实现动态控制页面元素的方法分析
2017/12/20 jQuery
基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果
2018/01/09 Javascript
在Vue中使用icon 字体图标的方法
2019/06/14 Javascript
Nodejs监控事件循环异常示例详解
2019/09/22 NodeJs
在vue中使用vuex,修改state的值示例
2019/11/08 Javascript
VUE实现自身整体组件销毁的示例代码
2020/01/13 Javascript
[45:50]完美世界DOTA2联赛PWL S3 CPG vs Forest 第二场 12.16
2020/12/17 DOTA
Python操作mysql数据库实现增删查改功能的方法
2018/01/15 Python
用Pelican搭建一个极简静态博客系统过程解析
2019/08/22 Python
Django之使用内置函数和celery发邮件的方法示例
2019/09/16 Python
python系统指定文件的查找只输出目录下所有文件及文件夹
2020/01/19 Python
keras获得model中某一层的某一个Tensor的输出维度教程
2020/01/24 Python
Python利用Faiss库实现ANN近邻搜索的方法详解
2020/08/03 Python
浅谈css3中的渐进增强和优雅降级
2017/12/01 HTML / CSS
HTML5之HTML元素扩展(下)—增强的Form表单元素值得关注
2013/01/31 HTML / CSS
弘扬职业精神演讲稿
2014/03/20 职场文书
房屋买卖协议书范本
2014/04/10 职场文书
机械电子工程专业自荐书
2014/06/10 职场文书
学校开学标语
2014/10/06 职场文书
单位接收函范文
2015/01/30 职场文书
单身申明具结书
2015/02/26 职场文书
浅谈移动端中的视口(viewport)的具体使用
2021/04/13 HTML / CSS