JS如何实现封装弹框插件,要点及方法是什么
Admin 2022-09-01 群英技术资讯 1035 次浏览
本篇内容介绍了“JS如何实现封装弹框插件,要点及方法是什么”的有关知识,在实际项目的操作过程或是学习过程中,不少人都会遇到这样的问题,接下来就让小编带大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!JavaScript封装弹框插件的具体代码,供大家参考,具体内容如下
知识点1、document.querySelector() 方法
querySelector() 方法返回文档中匹配指定 CSS 选择器的一个元素。
注意: querySelector() 方法仅仅返回匹配指定选择器的第一个元素。如果你需要返回所有的元素,请使用 querySelectorAll() 方法替代。
querySelectorAll() 方法返回文档中匹配指定 CSS 选择器的所有元素,返回 [NodeList] 对象。
知识点2、document.createElement() 用于创建一个元素
知识点3、innerHTML可获取或设置指定元素标签内的 html内容,从该元素标签的起始位置到终止位置的全部内容(包含html标签)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="../css/tanchuang.css" />
</head>
<body>
<button>
弹窗
</button>
<script>
var control = document.querySelector("button");
control.onclick = function() {
var shade = document.createElement("div");
shade.className = "shade";
shade.innerHTML = `
<div class="popwindows">
<div class="tltle">
<div class="text"><h3>温馨提示</h3></div>
<div class="exit"></div>
</div>
<div class="content"><h4>是否添加一个页面生成一个蓝色div</h4></div>
<div class="endbtn">
<div class="btn confirm">确定</div>
<div class="btn cancel">取消</div>
</div>
</div>
`
document.body.appendChild(shade);
var cancel = document.querySelector(".btn.cancel");
cancel.onclick = function() {
document.body.removeChild(shade);
}
var confirmDiv = document.querySelector(".btn.confirm");
confirmDiv.onclick = function() {
var a = document.createElement("div")
a.style.backgroundColor = "red";
a.style.width = "100px";
a.style.height = "100px";
document.body.appendChild(a);
document.body.removeChild(shade)
}
}
</script>
</body>
</html>
tanchuang.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.shade {
display: flex;
top: 0;
left: 0;
width: 100%;
height: 600px;
background-color: rgba(0, 0, 0, 0.5);
}
.shade .popwindows {
width: 400px;
height: 300px;
background-color: #f2f2f2;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
}
.shade .popwindows .tltle {
position: relative;
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
flex: 1;
border-bottom: 1px solid #bdb8b8;
}
.shade .popwindows .tltle .text {
flex: 1;
float: left;
padding-left: 10px;
font-family: "微软雅黑";
}
.shade .popwindows .tltle .exit {
width: 30px;
height: 30px;
background-image: url("../js学习/imag/cuohao.png");
background-repeat: no-repeat;
background-position: center center;
background-size: 20px auto;
float: right;
margin-right: 10px;
}
.shade .popwindows .content {
width: 100%;
flex: 3;
line-height: 40px;
font-size: 13px;
margin-left: 10px;
font-family: '宋体';
}
.shade .popwindows .endbtn {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
flex: 1;
border-top: 1px solid #bdb8b8;
}
.shade .popwindows .endbtn .btn{
width: 80px;
text-align: center;
height: 30px;
line-height: 30px;
font-size: 15px;
background-color: #979797;
}
.shade .popwindows .endbtn .btn:nth-child(1){
margin-right: 10px;
}
.shade .popwindows .endbtn .btn:nth-child(2){
margin-right: 10px;
}
.shade .popwindows .endbtn .btn:hover{
background-color: #f68c4e;
}

封装
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="../css/tanchuang.css" />
<script src="../js文件/popwindows.js"></script>
</head>
<body>
<button>添加弹窗</button>
</body>
<script>
var button = document.querySelector("button");
button.onclick = function() {
var args = {
title: "严重警告",
content: "您输入的内容不合法",
confirmDivfn: function() {
var a = document.createElement("div");
a.style.backgroundColor = "red";
a.style.width = "100px";
a.style.height = "100px";
document.body.appendChild(a);
},
cancelfn: function() {
}
};
Alert(args);
};
</script>
</html>
/*
var args = {
title:"温馨提示",
content:"是否添加一个页面生成一个蓝色div",
confirmDivfn:function(){
var a = document.createElement("div");
a.style.backgroundColor = "red";
a.style.width = "100px";
a.style.height = "100px";
body.appendChild(a);
},
cancelfn:function(){
body.removeChild(shade);
}
}
*/
function Alert(args) {
var shade = document.createElement("div");
shade.className = "shade";
shade.innerHTML =
`
<div class="popwindows">
<div class="tltle">
<div class="text"><h3>` +
args.title +
`</h3></div>
<div class="exit"></div>
</div>
<div class="content"><h4>` +
args.content +
`</h4></div>
<div class="endbtn">
<div class="btn confirm">确定</div>
<div class="btn cancel">取消</div>
</div>
</div>
`;
document.body.appendChild(shade);
var cancel = document.querySelector(".btn.cancel");
var confirmDiv = document.querySelector(".btn.confirm");
confirmDiv.onclick = function() {
/* 此处输入确认事件的内容*/
args.confirmDivfn();
document.body.removeChild(shade);
};
cancel.onclick = function() {
/* 此处输入取消事件的内容 */
args.cancelfn();
document.body.removeChild(shade);
};
};
css不变

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章我们来了解JavaScrip怎样判断回文数的相关内容,下文分享的示例是利用数组转为字符串数组,再用reserve()翻转数组,然后对于是否相等的方法来判断,下文我们来详细的了解看看实现思路及方法,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!
这篇文章主要给大家分享的是JavaScript 的反射学习技巧,主要是区别在于所有的函数对象属性过于复杂,而且额外增加可能会导致程序行为不合理,所以扩展 Reflect 函数来专门对函数对象处理调用方法,构造对象,获取或者设置属性等相关操作。下面一起进入文章内容吧
jquery实现轮播效果的方法:1、通过jquery的hover()绑定鼠标上悬以及离开时的事件处理;2、 通过jquery的bind()方法绑定鼠标点击事件处理前后翻动即可。
React返回页面有3种方式,分别是:1、通过“this.props.history.push('/home');”方式返回到上一级页面;2、通过“this.props.history.replace('/home');”方式返回页面;3、通过“window.history.back(-1);”返回页面。
这篇文章主要为大家详细介绍了JS实现圆形进度条拖拽滑动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008