Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JavaScript singleton mode to realize custom pop-up box

Shulou Source: shulou.com Published: 2022-06-03 07:59:16 11月15日 Update

This article mainly shows you "how to use JavaScript singleton mode to achieve custom pop-up box", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use JavaScript singleton mode to achieve custom pop-up box" this article.

Function

Custom pop-up box title

Custom pop-up box content

Callback function when custom pop-up box is confirmed and closed

Complete code const 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.le [XSS _ clean] = null const top = document.createElement ('div') top.className =' top' const topic = document.createElement ('span') topic.className =' topic' topic [XSS _ clean] = topicText const close = document.createElement ('span') close.className =' close' close [XSS _ clean] ='× 'top.appendChild (topic) top.appendChild (close) const middle = document.createElement ('div') middle.className =' middle' const content = document.createElement ('div') content.className =' content' content [XSS _ clean] = text middle.appendChild (content) const bottom = document.createElement ('div') bottom.className =' bottom' const confirm = document.createElement ('button') confirm.className =' confirm' account [XSS _ clean] = confirmText Const cancel = document.createElement ('button') cancel.className =' cancel' cancel [XSS _ clean] = 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:'OK', cancelText: 'cancel'}, res = > {console.log (res)})

Effect picture

The above is all the contents of the article "how to use the JavaScript singleton pattern to implement a custom pop-up box". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content pattern article title learning help code function function effect effect text easy to understand more organization knowledge editing industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Shulou Technology Huawei Redmi