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_15_作用域分配与变量访问规则,再送个闭包
Oct 20 Javascript
html组件不可输入(只读)同时任何组件都有效
Apr 01 Javascript
兼容FF和IE的动态table示例自写
Oct 21 Javascript
jqGrid日期格式的判断示例代码(开始日期与结束日期)
Nov 08 Javascript
js实现ArrayList功能附实例代码
Oct 29 Javascript
SyntaxHighlighter 3.0.83使用笔记
Jan 26 Javascript
Javascript动画效果(1)
Oct 11 Javascript
jQuery EasyUI Accordion可伸缩面板组件使用详解
Feb 28 Javascript
详解50行代码,Node爬虫练手项目
Apr 22 Javascript
关于vue 项目中浏览器跨域的配置问题
Nov 10 Javascript
vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件
Feb 20 Vue.js
如何通过简单的代码描述Angular父组件、子组件传值
Apr 07 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中去除所有js,html,css代码
2010/10/12 PHP
php中switch与ifelse的效率区别及适用情况分析
2015/02/12 PHP
php实现将字符串按照指定距离进行分割的方法
2015/03/14 PHP
php动态绑定变量的用法
2015/06/16 PHP
PHP封装的Twitter访问类实例
2015/07/18 PHP
yii2使用GridView实现数据全选及批量删除按钮示例
2017/03/01 PHP
PHP使用SWOOLE扩展实现定时同步 MySQL 数据
2017/04/09 PHP
PHP递归遍历文件夹去除注释并压缩php源代码的方法示例
2018/05/23 PHP
JQuery 表格操作(交替显示、拖动表格行、选择行等)
2009/07/29 Javascript
JS鼠标事件大全 推荐收藏
2011/11/01 Javascript
Jquery easyui 下loaing效果示例代码
2013/08/12 Javascript
JQuery筛选器全系列介绍
2013/08/27 Javascript
一个简单的JS时间控件示例代码(JS时分秒时间控件)
2013/11/22 Javascript
jquery做的一个简单的屏幕锁定提示框
2014/03/26 Javascript
node.js中的fs.fchownSync方法使用说明
2014/12/16 Javascript
学习JavaScript正则表达式
2015/11/13 Javascript
跟我学习javascript的函数和函数表达式
2015/11/16 Javascript
基于input框覆盖掉数字英文的实例讲解
2017/07/21 Javascript
了解JavaScript表单操作和表单域
2019/05/27 Javascript
Vue使用lodop实现打印小结
2019/07/06 Javascript
ES6 新增的创建数组的方法(小结)
2019/08/01 Javascript
Vue开发环境中修改端口号的实现方法
2019/08/15 Javascript
深入了解JavaScript 防抖和节流
2019/09/12 Javascript
关于layui导航栏不展示下拉列表的解决方法
2019/09/25 Javascript
Python实现去除代码前行号的方法
2015/03/10 Python
Python实现的质因式分解算法示例
2018/05/03 Python
Python实现的括号匹配判断功能示例
2018/08/25 Python
浅谈图像处理中掩膜(mask)的意义
2020/02/19 Python
python 获取当前目录下的文件目录和文件名实例代码详解
2020/03/10 Python
世界上最大的网络主机公司:1&1
2016/10/12 全球购物
美国沙龙美发产品购物网站:Hair.com by L’Oreal
2020/11/09 全球购物
英语专业学生个人求职信范文
2014/01/06 职场文书
书香校园活动方案
2014/02/28 职场文书
合作经营协议书范本
2014/04/17 职场文书
知识竞赛拉拉队口号
2014/06/16 职场文书
消费者投诉书范文
2015/07/02 职场文书