JS實現觀察者模式

來源:互聯網
上載者:User

標籤:style   http   color   io   java   ar   for   div   sp   

感謝自學IT網的燕十八老師

<!DOCTYPE HTML><html><head>    <meta http-equiv="content-type" content="text/html; charset=utf-8" />    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>觀察者模式</title>    <style type="text/css" media="all">        #sec{            margin:20px;        }        #cont,#ad,#study{            width:400px;            height:200px;            margin:10px;            border:1px solid #ccc;        }    </style>    </head><body>    <div class=‘container‘>        <select name="" id="sec">            <option value=‘male‘>男性</option>            <option value=‘famale‘>女性</option>        </select>        &nbsp;        <input type=‘button‘ value=‘觀察尾部‘ onclick=‘t1()‘ />        <input type=‘button‘ value=‘不觀察尾部‘ onclick=‘t2()‘ />        <div id=‘cont‘>這裡是內容</div>        <div id=‘ad‘>這裡是廣告</div>        <div id=‘study‘>這裡是學習</div>    </div></body>    <script type="text/javascript" charset="utf-8">        var sec = document.getElementById(‘sec‘);        var ad = document.getElementById(‘ad‘);        var cont = document.getElementById(‘cont‘);        var study = document.getElementById(‘study‘);        sec.observers = {};        sec.attach = function(key,obj){            this.observers[key] = obj;        }        sec.detach = function(key){            delete sec.observers[key];        }        sec.onchange = sec.notify = function(){            for(var key in this.observers){                this.observers[key].update(this);            }        }        cont.update = function(observer){            if (observer.value==‘male‘)             {                this.style.backgroundColor = ‘gray‘;            }else if (observer.value==‘famale‘){                this.style.backgroundColor = ‘pink‘;            }        }        ad.update = function(observer){            if (observer.value==‘male‘)             {                this.innerHTML = ‘汽車‘;            }else if (observer.value==‘famale‘){                this.innerHTML = ‘減肥‘;            }        }        study.update = function(observer){            if (observer.value==‘male‘)             {                this.innerHTML = ‘電腦‘;            }else if (observer.value==‘famale‘){                this.innerHTML = ‘化妝‘;            }        }        // 監聽        sec.attach(‘cont‘,cont);        sec.attach(‘ad‘,ad);        sec.attach(‘study‘,study);        function t1(){            // 觀察尾部            sec.attach(‘study‘,study);        }        function t2(){            // 不觀察尾部            sec.detach(‘study‘);        }    </script></html>



JS實現觀察者模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.