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 命名规则 变量命名规则
Feb 25 Javascript
jquery实现textarea输入字符控制(仿微博输入控制字符)
Apr 26 Javascript
JS输入用户名自动显示邮箱后缀列表的方法
Jan 27 Javascript
javascript DIV实现跟随鼠标移动
Mar 19 Javascript
JS判断来路是否是百度等搜索索引进行弹窗或自动跳转的实现代码
Oct 09 Javascript
微信小程序 POST请求(网络请求)详解及实例代码
Nov 16 Javascript
angular5 子组件监听父组件传入值的变化方法
Sep 30 Javascript
Vue用v-for给循环标签自身属性添加属性值的方法
Oct 18 Javascript
JS实现简单的文字无缝上下滚动功能示例
Jun 22 Javascript
layui实现给某一列加点击事件
Oct 26 Javascript
如何用JS实现简单的数据监听
May 06 Javascript
el-table-column 内容不自动换行的解决方法
Aug 14 Vue.js
通用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操作mysql执行数据库查询的一些常用操作汇总
2013/06/24 PHP
Linux下手动编译安装PHP扩展的例子分享
2014/07/15 PHP
smarty模板引擎中变量及变量修饰器用法实例
2015/01/22 PHP
PHP中error_reporting()用法详解
2015/08/31 PHP
PHP编程实现csv文件导入mysql数据库的方法
2017/04/29 PHP
新浪刚打开页面出来的全屏广告代码
2007/04/02 Javascript
神奇的代码 通杀各种网站-可随意修改复制页面内容
2008/07/17 Javascript
JQuery读取XML文件数据并显示的实现代码
2009/12/16 Javascript
javascript处理table表格的代码
2010/12/06 Javascript
jquery uaMatch源代码
2011/02/14 Javascript
jQuery动画效果-slideUp slideDown上下滑动示例代码
2013/08/28 Javascript
使用JavaScript修改浏览器URL地址栏的实现代码
2013/10/21 Javascript
window.onresize 多次触发的解决方法
2013/11/08 Javascript
jquery的live使用注意事项
2014/02/18 Javascript
JavaScript中的数组遍历forEach()与map()方法以及兼容写法介绍
2016/05/19 Javascript
AngularJS基础 ng-cloak 指令简单示例
2016/08/01 Javascript
Bootstrap模态框(modal)垂直居中的实例代码
2016/08/18 Javascript
js 判断登录界面的账号密码是否为空
2017/02/08 Javascript
Angular刷新当前页面的实现方法
2018/11/21 Javascript
vue实现多条件和模糊搜索功能
2019/05/28 Javascript
js判断一个对象是数组(函数)的方法实例
2019/12/19 Javascript
d3.js实现图形缩放平移
2019/12/19 Javascript
echarts饼图各个板块之间的空隙如何实现
2020/12/01 Javascript
python实现指定文件夹下的指定文件移动到指定位置
2018/09/17 Python
python dlib人脸识别代码实例
2019/04/04 Python
CSS3盒子模型详解
2013/04/24 HTML / CSS
计算机专业推荐信范文
2013/11/20 职场文书
网络编辑职责
2014/03/01 职场文书
爱情寄语大全
2014/04/09 职场文书
人力资源管理专业求职信
2014/07/23 职场文书
校长个人总结
2015/03/03 职场文书
财务部岗位职责范本
2015/04/14 职场文书
初中毕业感言300字
2015/07/31 职场文书
nginx 防盗链防爬虫配置详解
2021/03/31 Servers
使用 JavaScript 制作页面效果
2021/04/21 Javascript
JavaScript利用html5新方法操作元素类名详解
2021/11/27 Javascript