如何实现JavaScript单例模式下的自定义弹框功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计545个文字,预计阅读时间需要3分钟。
原文示例:本文字例为大家分享了JavaScript单例模式实现自定义弹框的具体代码,供大家参考。具体内容如下:
功能:自定义弹框自定义弹框自定义弹框内容自定义弹框确认和关闭时的回调函数完整代码:
javascript// 自定义弹框类function AlertBox(title, content, onConfirm, onClose) { this.title=title; this.content=content; this.onConfirm=onConfirm; this.onClose=onClose;}
// 显示弹框AlertBox.prototype.show=function() { console.log(':', this.title); console.log('内容:', this.content); if (this.onConfirm) { this.onConfirm(); } if (this.onClose) { this.onClose(); }};
// 使用示例var myAlert=new AlertBox('提示', '这是一个自定义弹框', function() { console.log('确认操作');}, function() { console.log('关闭弹框');});
myAlert.show();
本文实例为大家分享了JavaScript单例模式实现自定义弹框的具体代码,供大家参考,具体内容如下
功能
- 自定义弹框标题
- 自定义弹框内容
- 自定义弹框确认和关闭时的回调函数
完整代码
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: '标题', confirmText: '确定', cancelText: '取消' }, res => { console.log(res) })
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计545个文字,预计阅读时间需要3分钟。
原文示例:本文字例为大家分享了JavaScript单例模式实现自定义弹框的具体代码,供大家参考。具体内容如下:
功能:自定义弹框自定义弹框自定义弹框内容自定义弹框确认和关闭时的回调函数完整代码:
javascript// 自定义弹框类function AlertBox(title, content, onConfirm, onClose) { this.title=title; this.content=content; this.onConfirm=onConfirm; this.onClose=onClose;}
// 显示弹框AlertBox.prototype.show=function() { console.log(':', this.title); console.log('内容:', this.content); if (this.onConfirm) { this.onConfirm(); } if (this.onClose) { this.onClose(); }};
// 使用示例var myAlert=new AlertBox('提示', '这是一个自定义弹框', function() { console.log('确认操作');}, function() { console.log('关闭弹框');});
myAlert.show();
本文实例为大家分享了JavaScript单例模式实现自定义弹框的具体代码,供大家参考,具体内容如下
功能
- 自定义弹框标题
- 自定义弹框内容
- 自定义弹框确认和关闭时的回调函数
完整代码
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: '标题', confirmText: '确定', cancelText: '取消' }, res => { console.log(res) })
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

