欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript單例模式實(shí)現(xiàn)自定義彈框

 更新時(shí)間:2021年10月04日 10:16:51   作者:亂舞春秋__  
這篇文章主要為大家詳細(xì)介紹了JavaScript單例模式實(shí)現(xiàn)自定義彈框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JavaScript單例模式實(shí)現(xiàn)自定義彈框的具體代碼,供大家參考,具體內(nèi)容如下

功能

  • 自定義彈框標(biāo)題
  • 自定義彈框內(nèi)容
  • 自定義彈框確認(rèn)和關(guān)閉時(shí)的回調(diào)函數(shù)

完整代碼

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.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: '提示文字',
 topicText: '標(biāo)題',
 confirmText: '確定',
 cancelText: '取消'
 }, res => { console.log(res) })

效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js下用gb2312編碼解碼實(shí)現(xiàn)方法

    js下用gb2312編碼解碼實(shí)現(xiàn)方法

    在js中將中文用gb2312編碼。如,“我”編碼后應(yīng)該是“%CE%D2”。
    2009-12-12
  • 如何快速高效創(chuàng)建JavaScript 一維數(shù)組方法詳解

    如何快速高效創(chuàng)建JavaScript 一維數(shù)組方法詳解

    這篇文章主要為大家介紹了如何快速高效創(chuàng)建JavaScript 一維數(shù)組方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • EditPlus中的正則表達(dá)式 實(shí)戰(zhàn)(4)

    EditPlus中的正則表達(dá)式 實(shí)戰(zhàn)(4)

    這篇文章主要介紹了 EditPlus中的正則表達(dá)式 實(shí)戰(zhàn)(4)的相關(guān)資料,需要的朋友可以參考下
    2016-12-12
  • 最新評(píng)論