您现在的位置是:网站首页> 编程资料编程资料
JavaScript封装弹框插件的方法_javascript技巧_
2023-05-24
425人已围观
简介 JavaScript封装弹框插件的方法_javascript技巧_
JavaScript封装弹框插件的具体代码,供大家参考,具体内容如下
知识点1、document.querySelector() 方法
querySelector() 方法返回文档中匹配指定 CSS 选择器的一个元素。
注意: querySelector() 方法仅仅返回匹配指定选择器的第一个元素。如果你需要返回所有的元素,请使用 querySelectorAll() 方法替代。
querySelectorAll() 方法返回文档中匹配指定 CSS 选择器的所有元素,返回 [NodeList] 对象。
知识点2、document.createElement() 用于创建一个元素
知识点3、innerHTML可获取或设置指定元素标签内的 html内容,从该元素标签的起始位置到终止位置的全部内容(包含html标签)。
Document
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; }
封装
Document
/* 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 = ` ` + args.title + `
` + args.content + `
确定 取消 `; 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不变

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
您可能感兴趣的文章:
相关内容
- vue实现点击复制到粘贴板_vue.js_
- vue-router中关于children的使用方法_vue.js_
- Web js实现复制文本到粘贴板_javascript技巧_
- 使用js实现复制功能_javascript技巧_
- Vue中实现v-for循环遍历图片的方法_vue.js_
- Vue中对数组和对象进行遍历和修改方式_vue.js_
- 如何全局重写小程序Page函数wx对象详解_javascript技巧_
- Vue计算属性与监视属性实现方法详解_vue.js_
- vue深度优先遍历多层数组对象方式(相当于多棵树、三级树)_vue.js_
- JS判断当前是否平板安卓并是否支持cordova方法的示例代码_javascript技巧_
