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)文章
紅黑樹的插入詳解及Javascript實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于紅黑樹的插入的相關(guān)資料,以及Javascript實(shí)現(xiàn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧。2018-03-03JavaScript中高級(jí)語(yǔ)法??表達(dá)式用法示例詳解
這篇文章主要為大家介紹了JavaScript中高級(jí)語(yǔ)法??表達(dá)式用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

如何快速高效創(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)的相關(guān)資料,需要的朋友可以參考下
2016-12-12