js 面向对象设计模式--模板模式

24 篇文章 0 订阅
23 篇文章 0 订阅
//创建一个提示框类
let Alert = function (data) {
    //没有数据则返回,让后面的程序执行
    if(!data)
        return;
    //设置内容
    this.content = data.content;
    //创建提示框面板
    this.panel = document.createElement('div');
    //创建提示内容模块
    this.content_panel = document.createElement('div');
    //创建确定按钮组件
    this.confirmBtn =document.createElement('span');
    //创建提示内容组件
    this.contentNode = document.createElement('p');
    //创建关闭按钮
    this.closeBtn = document.createElement('span');
//    为提示面板添加类
    this.panel.className = 'alert';
    //为提示内容添加提示类
    this.content_panel.className = "content";
    //为确定按钮添加类
    this.confirmBtn.className = "a_confirm";
    //为关闭按钮添加类
    this.closeBtn.className = "c_close";
    //为确认按钮添加文案
    this.confirmBtn.innerHTML = data.confirm||'确认';
    this.closeBtn.innerHTML = data.close||'取消';
    //为提示框添加文本
    this.contentNode.innerHTML = this.content;
    //
    this.success = data.success||function () {

    };
    this.fail = data.fail || function () {

    };
    //创建提示框的原型方法

}
Alert.prototype = {
    init:function () {
        //初始化方法
        this.panel.appendChild(this.content_panel);
        this.content_panel.appendChild(this.contentNode);
        this.content_panel.appendChild(this.closeBtn);
        this.content_panel.appendChild(this.confirmBtn);


        //将dom语句插入页面中
        document.body.appendChild(this.panel);
        //绑定事件
        this.bindEvent();
        //显示提示框
        this.show();
    },
    bindEvent:function () {
        let me = this;
        this.closeBtn.onclick = function () {
            //执行关闭取消方法
            me.fail();
            //隐藏弹框
            me.hide();
        }
        this.confirmBtn.onclick = function () {
            me.success();
            me.hide();
        }
    },
    hide:function () {
        this.panel.style.display = 'none';
    },
    show:function () {
        this.panel.style.display = 'block';
    }

};
let RightAlert = function (data){
    Alert.call(this,data);
    this.confirmBtn.className = this.confirmBtn.className+" right";
}
RightAlert.prototype = new Alert();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_42975115

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值