Js订制自己的AlertBox

本文制作一个用户自定义的AlertBox,效果如图:
Js订制自己的AlertBox_第1张图片
js文件中插入如下代码:
  1. // JScript 文件
  2.  // constants to define the title of the alert and button text.
  3. var ALERT_TITLE = "Oops!";
  4. var ALERT_BUTTON_TEXT = "Close";
  5. // over-ride the alert method only if this a newer browser.
  6. // Older browser will see standard alerts
  7. if(document.getElementById) {
  8.   window.alert = function(txt) {
  9.     createCustomAlert(txt);
  10.   }
  11. }
  12. function createCustomAlert(txt) {
  13.   // shortcut reference to the document object
  14.   d = document;
  15.   // if the modalContainer object already exists in the DOM, bail out.
  16.   if(d.getElementById("modalContainer")) return;
  17.   // create the modalContainer div as a child of the BODY element
  18.   mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
  19.   mObj.id = "modalContainer";
  20.    // make sure its as tall as it needs to be to overlay all the content on the page
  21.   mObj.style.height = document.documentElement.scrollHeight + "px";
  22.   // create the DIV that will be the alert
  23.   alertObj = mObj.appendChild(d.createElement("div"));
  24.   alertObj.id = "alertBox";
  25.   // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
  26.   if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
  27.   // center the alert box
  28.   alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
  29.   // create an H1 element as the title bar
  30.   h1 = alertObj.appendChild(d.createElement("h1"));
  31.   h1.appendChild(d.createTextNode(ALERT_TITLE));
  32.   // create a paragraph element to contain the txt argument
  33.   msg = alertObj.appendChild(d.createElement("p"));
  34.   msg.innerHTML = txt;
  35.   // create an anchor element to use as the confirmation button.
  36.   btn = alertObj.appendChild(d.createElement("a"));
  37.   btn.id = "closeBtn";
  38.   btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
  39.   btn.href = "#";
  40.   // set up the onclick event to remove the alert when the anchor is clicked
  41.   btn.onclick = function() { removeCustomAlert();return false; }
  42. // removes the custom alert from the DOM function removeCustomAlert() {
  43. //  document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
  44. }
  45. // removes the custom alert from the DOM
  46. function removeCustomAlert() 
  47. {
  48.     document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
  49. }

将如下代码粘贴到你的HTML你的HTML的HEAD部分。
  1.     <script type="text/javascript" src="include/customAlertBox.js"></script>
  2. <!-- Paste this code into your external style sheet or the
  3.      CSS section of your HTML document  -->
  4.     <style type="text/css">
  5. #modalContainer {
  6.     background-color:transparent;
  7.     position:absolute;
  8.     width:100%;
  9.     height:100%;
  10.     top:0px;
  11.     left:0px;
  12.     z-index:10000;
  13. }
  14. #alertBox {
  15.     position:relative;
  16.     width:300px;
  17.     min-height:100px;
  18.     margin-top:50px;
  19.     border:2px solid #000;
  20.     background-color:#F2F5F6;
  21.     background-image:url(alert.png);
  22.     background-repeat:no-repeat;
  23.     background-position:20px 30px;
  24. }
  25. #modalContainer > #alertBox {
  26.     position:fixed;
  27. }
  28. #alertBox h1 {
  29.     margin:0;
  30.     font:bold 0.9em verdana,arial;
  31.     background-color:#78919B;
  32.     color:#FFF;
  33.     border-bottom:1px solid #000;
  34.     padding:2px 0 2px 5px;
  35. }
  36. #alertBox p {
  37.     font:0.7em verdana,arial;
  38.     height:50px;
  39.     padding-left:5px;
  40.     margin-left:55px;
  41. }
  42. #alertBox #closeBtn {
  43.     display:block;
  44.     position:relative;
  45.     margin:5px auto;
  46.     padding:3px;
  47.     border:1px solid #000;
  48.     width:70px;
  49.     font:0.7em verdana,arial;
  50.     text-transform:uppercase;
  51.     text-align:center;
  52.     color:#FFF;
  53.     background-color:#78919B;
  54.     text-decoration:none;
  55. }
  56. </style>
在你的HTML文档的Body部分插入如下代码:
  1. <input type="button" value = "Test the alert" onclick="alert('This is a custom alert dialog that was created by overriding the window.alert method.');">
原码地址:http://javascript.internet.com/miscellaneous/custom-alert-box.html

你可能感兴趣的:(Js订制自己的AlertBox)