设计模式

#content,#ad,#study{

width:200px;

height:100px;

border:1px solid black;

}

observe

male

female

content

ad

//select为被观察  下面内容为观察

varsel=document.getElementsByTagName('select')[0];

sel.observers={}

//把观察的记录号

sel.attach=function(key,obj){

sel.observers[key]=obj;

}

sel.detach=function(key){

deletethis.observers[key];

}

sel.onchange=sel.notify=function() {

for(varkey inthis.observers){

this.observers[key].update(this);

}

}

varcontent=document.getElementById('content');

varad=document.getElementById('ad');

content.update=function(observee) {

if(observee.value=='male'){

this.innerHTML='car';

}else if(observee.value='female'){

this.innerHTML='jianfei';

}

}

ad.update=function(observee) {

if(observee.value=='male'){

this.innerHTML='male_ad';

}else if(observee.value='female'){

this.innerHTML='female_ad';

}

}

//让content观察select的变化

sel.attach('content',content);

functiont1(){

sel.attach('ad',ad);

}

functiont2(){

sel.detach('ad');

}

你可能感兴趣的:(设计模式)