This article shares the specific code of JavaScript singleton mode to implement custom pop-up boxes for your reference. The specific content is as follows Function
Complete codeconst Dialog = (function () { class Dialog { constructor () { this.ele = document.createElement('div') this.ele.className = 'dialog' document.body.appendChild(this.ele) this.callback = null this.setEvent() } setContent ({ text, topicText, confirmText, cancelText } = options) { this.ele.innerHTML = null const top = document.createElement('div') top.className = 'top' const topic = document.createElement('span') topic.className = 'topic' topic.innerHTML = topicText const close = document.createElement('span') close.className = 'close' close.innerHTML = '×' top.appendChild(topic) top.appendChild(close) const middle = document.createElement('div') middle.className = 'middle' const content = document.createElement('div') content.className = 'content' content.innerHTML = text middle.appendChild(content) const bottom = document.createElement('div') bottom.className = 'bottom' const confirm = document.createElement('button') confirm.className = 'confirm' confirm.innerHTML = confirmText const cancel = document.createElement('button') cancel.className = 'cancel' cancel.innerHTML = cancelText bottom.appendChild(confirm) bottom.appendChild(cancel) const wrap = document.createElement('div') this.ele.appendChild(top) this.ele.appendChild(middle) this.ele.appendChild(bottom) this.ele.style.display = 'block' } setEvent () { this.ele.addEventListener('click', e => { e = e || window.event const target = e.target || e.srcElement if (target.className === 'close') { this.ele.style.display = 'none' console.log('close') } if (target.className === 'confirm') { this.ele.style.display = 'none' this.callback(true) } if (target.className === 'cancel') { this.ele.style.display = 'none' this.callback(false) } }) } } let instance = null return function (options, cb) { if (!instance) instance = new Dialog() instance.setContent(options) instance.callback = cb || function () {} return instance } })() const dialog = new Dialog({ text: 'prompt text', topicText: 'Title', confirmText: 'Confirm', cancelText: 'Cancel' }, res => { console.log(res) }) Rendering The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Sharing experience on MySQL slave maintenance
>>: Proxy_pass method in multiple if in nginx location
Windows installation mysql-5.7.17-winx64.zip meth...
This article shares the specific code of JavaScri...
Table of contents 1. Introduction to the basic fu...
Summary: What method should be used for MySQL JDB...
As a front-end novice, I tinkered with the front-e...
Hexo binds a custom domain name to GitHub under W...
1|0MySQL (MariaDB) 1|11. Description MariaDB data...
Brief Tutorial This is a CSS3 color progress bar ...
Table of contents Application scenarios: Method 1...
SQLyog connects to mysql error number 1129: mysql...
Table of contents Installation-free version of My...
The previous articles introduced how to debug loc...
Table of contents Vue CLI builds a Vue project Vu...
introduction In recent years, the call for TypeSc...
When installing packages on an Ubuntu server, you...