复制头部的 js 代码到你的 js 文件的任何地方,调用Chef.alert方法传入相应的参数即可并没有什么功能,只是一个提示的作用,可能样式比 alert 的弹窗好看点,css是写在js里的,只要你会写 css 就可以自行修改样式.

Chef.alert 使用说明:

此方法有6个参数:
1,title 弹出框的标题
2,content 弹出框的提示文字也可以以字符串的形式传入任何html标签,
3,firm 弹出框按钮的文字
4,offset 弹出框距离顶部的位置,左右默认水平居中,
5,width 弹出框的宽度
6,shade 遮罩层的透明度

觉得没有用的参数可以不传
******
注意 :Chef.alert 只是一个提示的作用 没有任何操作

以下是代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
var Chef = {
//body 的宽高
'bodyH':document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight,
'bodyW':document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth,
//动态创建 style 标签添加样式
'cssStyle':function (){
var doc=document;
var style=doc.createElement("style");
if(style.styleSheet){// IE
style.styleSheet.cssText = arguments[0];
}else{// w3c
var cssText = doc.createTextNode(arguments[0]);
style.appendChild(cssText);
}
var heads = doc.getElementsByTagName("head");
if(heads.length){
heads[0].appendChild(style);
}else{
doc.documentElement.appendChild(style);
}
},
// 创建并显示遮罩层
'createChef':function(){
if(document.body.getElementsByClassName('Chef_opacity').length == 1){
document.body.removeChild(document.body.getElementsByClassName('Chef_opacity')[0]);
}
var div = this.create('div');
div.style.width = this.bodyW + 'px';
div.style.height = this.bodyH + 'px';
div.className = 'Chef_opacity';
document.body.appendChild(div);
},
//alert 框
'alert':function(){
// 显示遮罩层
this.createChef();
// 创建
var alertDiv = this.create('div'),
alertH2 = this.create('h2'),
alertX = this.create('span'),
alertP = this.create('p'),
alertBDiv = this.create('div'),
alertFirm = this.create('button');
alertX.innerHTML = 'X';
alertX.className = 'Chef_X';
// 插号的click事件 什么都不做
alertX.onclick = function(){alertFirm.onclick();}
// 确定按钮的click事件 什么都不做
alertFirm.onclick = function(){
document.getElementsByClassName('Chef_opacity')[0].style.display = 'none';
document.body.removeChild(alertDiv);
} //样式以及内容
alertDiv.className = 'Chef_alert';
if(arguments.length == 1){
document.getElementsByClassName('Chef_opacity')[0].style.background = 'rgba(0,0,0,'+arguments[0].shade+')' ;
alertDiv.style.top = arguments[0].offset;
if(arguments[0].width == undefined){
alertDiv.style.width = '260px';
}else{
alertDiv.style.width = arguments[0].width;
alertDiv.style.marginLeft = '-'+parseInt(arguments[0].width)/2 + 'px';
}
arguments[0].title == undefined ? alertH2.innerHTML = '来自网页的信息' : alertH2.innerHTML = arguments[0].title;
arguments[0].content == undefined ? alertP.innerHTML = '' : alertP.innerHTML = arguments[0].content;
arguments[0].firm == undefined ? alertFirm.innerHTML = '确定' : alertFirm.innerHTML = arguments[0].firm;
}else{// -- 默认提示信息
alertH2.innerHTML = '来自网页的信息';
alertFirm.innerHTML = '确定';
}
// 添加到页面
alertBDiv.appendChild(alertFirm);
alertH2.appendChild(alertX);
alertDiv.appendChild(alertH2);
alertDiv.appendChild(alertP);
alertDiv.appendChild(alertBDiv);
document.body.appendChild(alertDiv);
},
//创建
'create':function(){
return document.createElement(arguments[0]);
}
};
;(function(Chef){
var cssString = '\
*{padding:0;margin:0;}\
.Chef_opacity{display:block;background:rgba(0,0,0,0.4);position:fixed;top:0;z-index:99;}\
.Chef_alert{position:fixed;top:100px;background:white;border-top:3px solid #FF6636;width:260px;padding-bottom:5px;left:50%;margin-left:-130px;z-index:100;font-family:Microsoft YaHei;}\
.Chef_alert>h2{width:90%;margin:10px auto;margin-bottom:0;font-size:18px;}\
.Chef_alert>p{width:90%;margin:0 auto;padding:25px 0;border-bottom:1px solid #d8d8d8;}\
.Chef_alert>div{width:90%;height:60px;margin:0 auto;font-size:0;text-align: center;}\
.Chef_alert>div>button{width:50%;height:100%;border:0;outline:0;font-size:18px;color:#FE651F;background:white;font-family:Microsoft YaHei;cursor:pointer;}\
.Chef_X{float:right;font-size:13px;color:grey;cursor:pointer;font-weight:normal;}\
';
Chef.cssStyle(cssString);
})(Chef);
</script>
</head>
<body>
<button id='alertBtn'>alert弹窗</button>
<script>
//获取对象添加事件
document.getElementById('alertBtn').onclick = function(){
//调用 Chef.alert() 方法
Chef.alert({
'title':'标题标题标题',
'content':'内容',
'firm':'确定',
'offset':'100px',
'width':'260px',
'shade':0.4
});
};
</script>
</body>
</html>

有问题可以留言咨询,看到一定回复。

原生 js 模拟 alert 弹窗的更多相关文章

  1. JS模拟Alert与Confirm对话框

    这2个例子都是用原生JS写的,主要是用JS拼接了界面,并未做过多的事件监听.,样式用了Css3的一些特性. 调用方式则为: //Alert Alert.show('我警告你哦~'); //Confir ...

  2. 原生js模拟jquery写法

    function $_custom(fun) { document.onreadystatechange = function() { if (document.readyState == " ...

  3. 原生JS模拟jQuery $

    模拟jQuery的$选择器 在获取元素的时候使用ID选择器,返回的是一个对象:使用类选择器或者标签选择器返回可能是一组元素:将获取到的一个或一组元素进行一个简易的封装封装成一个TQObject 什么是 ...

  4. js重写alert()弹窗

    //重写alertwindow.alert = function(str){ var alertFram = document.getElementById('alertFram'); var shi ...

  5. 原生js模拟jquery中的addClass和removeClass方法

    js代码: //添加类 function addClass(obj,className) { if(obj.className == '') { //如果没有class obj.className = ...

  6. CEF拦截js层alert弹窗 OnJSDialog 《转》

    一 引言 CEF3嵌入后,用JS 弹出Alert框,按钮错位,确定按钮勉强能看到.很难看.为了改善体验,决定重写提示框. 环境:VS2008  VC  MFC.   二 原理 参看类 CefJSDia ...

  7. 原生js控制控制--弹窗的显示和隐藏

    以防浪费大家的时间,还是先上效果图吧,满足您的需求就往下look吧. 重要知识点:点击其他地方,也就是除了小叉子之外的地方也能够关闭弹窗哦.代码已标红    html代码: <button id ...

  8. 使用原生js模拟jQuery选择器,实现new方法,兼容ie5

    // 考虑到兼容ie5,未使用es6语法 /* 使用方法: 在<head>标签中(需使用ready方法): <script src="./jQuery2.js"& ...

  9. javascript项目实战之原生js模拟淘宝购物车

    通过JavaScript实现类似与淘宝的购物车效果,包括商品的单选.全选.删除.修改数量.价格计算.数目计算.预览等功能的实现.实现的效果图: 相应的代码: shoppingCart.html < ...

随机推荐

  1. UliPad双击没反应,UliPad打不开

    关于这个问题呢我也是蛋疼了好久,前几天是把这东西卸了重装,然后莫名其妙就可以了. 今天又遇到这问题,第一个想到的也是重装,发现不行,于是就搜了下,发现果然是网能的网友,下面贴图: 经过本屌几次尝试,鉴 ...

  2. js判断手机端操作系统(Andorid/IOS),并自动为链接添加相应下载地址

    <script type="text/javascript"> $(document).ready(function(e) { var u = navigator.us ...

  3. 把Tomcat做成系统服务自动启动

    用Tomcat的bin目录下的service.bat,cmd,命令:进入到Tomcat的bin目录 service.bat install可以把tomcat做成系统服务;修改下计算机管理里面的服务,找 ...

  4. 存储过程使用CTE 和 case when

    未用SQL CTE and case when: ALTER PROCEDURE [dbo].[usp_rptDropboxBatchSummary1] )='ALL', )='ALL', )='AL ...

  5. [Android Pro] svn实例

    referece : http://www.cnblogs.com/cnblogsfans/archive/2010/03/21/1690891.html 签出 svn checkout URL pa ...

  6. August 22nd 2016 Week 35th Monday

    Have you ever given any thought to your future? 你有没有为将来打算过呢? Have you ever given any thought to your ...

  7. 立方体旋转 【web前端学习部落22群120342833】

    效果: HTML部分: <body class="body"> <div class="rect-wrap">   <!-- // ...

  8. C# 类中索引器的使用二

    索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便.直观的被引用.索引器非常类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用.定义 ...

  9. Mysql游标

    14.6.6.1 Cursor CLOSE Syntax 14.6.6.2 Cursor DECLARE Syntax 14.6.6.3 Cursor FETCH Syntax 14.6.6.4 Cu ...

  10. 【131031】jsp学习实例 (2013-10-31 15:29:28)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@ page language= ...