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弹性滑动导航菜单实现思路及代码
May 02 Javascript
file控件选择上传文件确定后触发的js事件是哪个
Mar 17 Javascript
js用typeof方法判断undefined类型
Jul 15 Javascript
js智能获取浏览器版本UA信息的方法
Aug 08 Javascript
基于jQuery实现表格内容的筛选功能
Aug 21 Javascript
原生js实现回复评论功能
Jan 18 Javascript
js模块加载方式浅析
Aug 12 Javascript
babel7.x和webpack4.x配置vue项目的方法步骤
May 12 Javascript
快速解决layui弹窗按enter键不停弹窗的问题
Sep 18 Javascript
vue 实现路由跳转时更改页面title
Nov 05 Javascript
jquery实现垂直手风琴导航栏
Feb 18 jQuery
部署vue+Springboot前后端分离项目的步骤实现
May 31 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使用ffmpeg获取视频信息并截图的实现方法
2016/05/03 PHP
php版阿里云OSS图片上传类详解
2016/12/01 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题
2019/10/15 PHP
动态为事件添加js代码示例
2009/02/15 Javascript
JavaScript 小型打飞机游戏实现原理说明
2010/10/28 Javascript
js的写法基础分析
2011/01/17 Javascript
js获取select默认选中的Option并不是当前选中值
2014/05/07 Javascript
JavaScript中的包装对象介绍
2015/01/27 Javascript
javascript中call apply 的应用场景
2015/04/16 Javascript
Vue.js双向绑定实现原理详解
2016/12/22 Javascript
js中数组的常用方法小结
2016/12/30 Javascript
Bootstrap学习笔记之进度条、媒体对象实例详解
2017/03/09 Javascript
JavaScript数据结构中串的表示与应用实例
2017/04/12 Javascript
Vue2几种常见开局方式详解
2017/09/09 Javascript
JS实现将链接生成二维码并转为图片的方法
2018/03/17 Javascript
vue+webpack实现异步加载三种用法示例详解
2018/04/24 Javascript
vue实现自定义多选与单选的答题功能
2018/07/05 Javascript
使用layui的router来进行传参的实现方法
2019/09/06 Javascript
JS中准确判断变量类型的方法
2020/06/01 Javascript
Windows系统下安装Python的SSH模块教程
2015/02/05 Python
pymongo为mongodb数据库添加索引的方法
2015/05/11 Python
Python如何优雅获取本机IP方法
2019/11/10 Python
Pytorch之contiguous的用法
2019/12/31 Python
Python代码需要缩进吗
2020/07/01 Python
html5中监听canvas内部元素点击事件的三种方法
2019/04/28 HTML / CSS
固特异美国在线轮胎店:Goodyear Tire
2019/02/23 全球购物
工程造价与管理专业应届生求职信
2013/11/23 职场文书
工作会议欢迎词
2014/01/16 职场文书
法学个人求职信范文
2014/01/27 职场文书
大学生党员学习焦裕禄精神思想汇报
2014/09/10 职场文书
2014年督导工作总结
2014/11/19 职场文书
道德模范事迹材料
2014/12/20 职场文书
2016年世界艾滋病日宣传活动总结
2016/04/01 职场文书
HTML+VUE分页实现炫酷物联网大屏功能
2021/05/27 Vue.js
SQL SERVER实现连接与合并查询
2022/02/24 SQL Server