模版方法模式

        // 接下來寫一個 類似的layer彈框插件;
        function Layer (data){
            if(!data){
                return;
            }          
            // 創(chuàng)建HTML頁面代碼
            // 創(chuàng)建最外層盒子 樣式寫在 common.css中
            this.LayerBox = document.createElement('div');
            this.LayerBox.className = 'layerBox';
            // 創(chuàng)建一個title 盒子
            this.LayerTitle = document.createElement('div'); 
            this.LayerTitle.className = "layerTitle";
            // 創(chuàng)建內(nèi)容盒子
            this.LayerContent = document.createElement('div');
            this.LayerContent.className = "layerContent"; 
            // 創(chuàng)建一個 確定按鈕盒子
            this.LayerConfirmBox = document.createElement('div'); 
            this.LayerConfirmBox.className = "layerConfirmBox";
            this.LayerConfirmBtn = document.createElement('a');
            // 創(chuàng)建關(guān)閉彈框按鈕HTML代碼
            this.LayerCloseBox = document.createElement('span');
            this.LayerCloseBox.className = "layerCloseBox";
            this.LayerCloseIcon = document.createElement('i');
            // 創(chuàng)建朦朧層HTML代碼
            this.LayerHazyBox = document.createElement('div');
            this.LayerHazyBox.className = "layerHazyBox"
            // 定義盒子內(nèi)容
            this.title = data.title;
            this.content = data.content; 
            this.btnText = data.btnText;
            this.LayerTitle.innerHTML = this.content || "信息";
            this.LayerConfirmBtn.innerHTML = this.btnText || "確定";
            this.LayerContent.innerHTML = this.content || "默認(rèn)信息";
            // 添加一個確定后的回調(diào)函數(shù)
            this.success = data.success || function(){};
            // 添加一個關(guān)閉彈框的回調(diào)函數(shù)
            this.close = data.close || function(){};
        }
        Layer.prototype = {
            init : function(){
                this.LayerBox.appendChild(this.LayerTitle);
                this.LayerBox.appendChild(this.LayerContent);
                this.LayerBox.appendChild(this.LayerConfirmBox);
                this.LayerBox.appendChild(this.LayerCloseBox);
                this.LayerCloseBox.appendChild(this.LayerCloseIcon);
                this.LayerConfirmBox.appendChild(this.LayerConfirmBtn);
                document.body.appendChild(this.LayerBox);
                document.body.appendChild(this.LayerHazyBox);
                // 讓彈框出現(xiàn)
                this.show();
                // 確定跟關(guān)閉盒子綁定事件
                this.bindEvent();
            },
            bindEvent : function(){
                // 給 確定 跟 x 綁定事件
                let _this = this;
                this.LayerConfirmBtn.onclick = function(){
                    _this.success();
                    _this.hide();
                }
                this.LayerCloseIcon.onclick = function(){
                    _this.close();
                    _this.hide();
                }
            },
            show : function(){
                this.LayerBox.style.display = "block";
                this.LayerHazyBox.style.display = "block";
            },
            hide : function(){
                this.LayerBox.style.display = "none";
                this.LayerHazyBox.style.display = "none";
                // 讓HTML代碼刪除
                document.body.removeChild(this.LayerBox);
                document.body.removeChild(this.LayerHazyBox);
            }
        }

        var btn = document.getElementById('btn');
        btn.onclick = function(){
            new Layer({
                title:"",
                content:"你好灸拍!",
                success:function(){
                    alert('確定后執(zhí)行函數(shù)');
                },
                close:function(){
                    alert('點擊關(guān)閉執(zhí)行函數(shù)');
                }
            }).init();
        }

        // 使用子類繼承 父類 抽象 改變父類結(jié)構(gòu)
        // 給確定按鈕 添加一個取消按鈕
        function LayerInquiry (data){
            // 改變this指向
            Layer.call(this,data);
            // 這里this只的是父級 Layer
            // 創(chuàng)建一個取消按鈕
            this.LayerCloseBtn = document.createElement('a');
            this.btnCloseText = data.btnCloseText;
            this.LayerCloseBtn.innerHTML = this.btnCloseText || "取消";
        }
        // 原型鏈繼承父級所有方法
        LayerInquiry.prototype = new Layer();
        LayerInquiry.prototype.init = function(){
            // 改變初始化  取消按鈕添加到盒子
            this.LayerConfirmBox.appendChild(this.LayerCloseBtn);
            // 繼承父級原始的 init方法做祝;
            Layer.prototype.init.call(this);
        }
        LayerInquiry.prototype.bindEvent = function(){
            let _this = this;
            // 為創(chuàng)建的取消按鈕 添加綁定事件
            this.LayerCloseBtn.onclick = function(){
                _this.hide();
            }
            // 繼承父級原始的  bindEvent方法;
            Layer.prototype.bindEvent.call(this);
        }
        var btn2 = document.getElementById('btn2');
        btn2.onclick = function(){
            new LayerInquiry({
                title:"添加需求的Title",
                content:"添加取消了",
                success:function(){
                    alert('確定后執(zhí)行函數(shù)');
                },
                close:function(){
                    alert('點擊關(guān)閉執(zhí)行函數(shù)');
                }
            }).init();
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鸡岗,一起剝皮案震驚了整個濱河市混槐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌轩性,老刑警劉巖声登,帶你破解...
    沈念sama閱讀 211,376評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異揣苏,居然都是意外死亡悯嗓,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評論 2 385
  • 文/潘曉璐 我一進店門卸察,熙熙樓的掌柜王于貴愁眉苦臉地迎上來脯厨,“玉大人,你說我怎么就攤上這事坑质『衔洌” “怎么了临梗?”我有些...
    開封第一講書人閱讀 156,966評論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長稼跳。 經(jīng)常有香客問我盟庞,道長,這世上最難降的妖魔是什么汤善? 我笑而不...
    開封第一講書人閱讀 56,432評論 1 283
  • 正文 為了忘掉前任茫经,我火速辦了婚禮,結(jié)果婚禮上萎津,老公的妹妹穿的比我還像新娘卸伞。我一直安慰自己,他們只是感情好锉屈,可當(dāng)我...
    茶點故事閱讀 65,519評論 6 385
  • 文/花漫 我一把揭開白布荤傲。 她就那樣靜靜地躺著,像睡著了一般颈渊。 火紅的嫁衣襯著肌膚如雪遂黍。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,792評論 1 290
  • 那天俊嗽,我揣著相機與錄音雾家,去河邊找鬼。 笑死绍豁,一個胖子當(dāng)著我的面吹牛芯咧,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播竹揍,決...
    沈念sama閱讀 38,933評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼敬飒,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了芬位?” 一聲冷哼從身側(cè)響起无拗,我...
    開封第一講書人閱讀 37,701評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎昧碉,沒想到半個月后英染,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,143評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡被饿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,488評論 2 327
  • 正文 我和宋清朗相戀三年四康,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锹漱。...
    茶點故事閱讀 38,626評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡箭养,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出哥牍,到底是詐尸還是另有隱情毕泌,我是刑警寧澤喝检,帶...
    沈念sama閱讀 34,292評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站撼泛,受9級特大地震影響挠说,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜愿题,卻給世界環(huán)境...
    茶點故事閱讀 39,896評論 3 313
  • 文/蒙蒙 一损俭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧潘酗,春花似錦杆兵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缸兔,卻和暖如春日裙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背惰蜜。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工昂拂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人抛猖。 一個月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓格侯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親樟结。 傳聞我的和親對象是個殘疾皇子养交,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,494評論 2 348

推薦閱讀更多精彩內(nèi)容