用观察者模式
来源:互联网 发布:淘宝客淘口令怎么设置 编辑:程序博客网 时间:2024/05/01 01:18
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用观察者模式来切换</h1>
<select>
<option value="male">男士风格</option>
<option value="female">女士风格</option>
</select>
<div id="content" style="width:100px; height: 100px; border:1px solid #000;"></div>
<script type="text/javascript">
var sel=document.getElementsByTagName('select')[0];
sel.observers={};
sel.attach=function(key,obj){ //
sel.observers[key]=obj;
}
sel.detach=function(key){
delete observers[key];
}
sel.onchange=sel.notify=function(){
console.log(this.observers);
for(var key in this.observers){
this.observers[key].update(this);
}
}
//客户端
var content=document.getElementById("content");
content.update=function(obs){
if(obs.value=='male'){
this.style.backgroundColor='gray';
}else if(obs.value='female'){
this.style.backgroundColor='red';
}
}
//让content 观察select 变化
sel.attach('content',content);
</script>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>用观察者模式来切换</h1>
<select>
<option value="male">男士风格</option>
<option value="female">女士风格</option>
</select>
<div id="content" style="width:100px; height: 100px; border:1px solid #000;"></div>
<script type="text/javascript">
var sel=document.getElementsByTagName('select')[0];
sel.observers={};
sel.attach=function(key,obj){ //
sel.observers[key]=obj;
}
sel.detach=function(key){
delete observers[key];
}
sel.onchange=sel.notify=function(){
console.log(this.observers);
for(var key in this.observers){
this.observers[key].update(this);
}
}
//客户端
var content=document.getElementById("content");
content.update=function(obs){
if(obs.value=='male'){
this.style.backgroundColor='gray';
}else if(obs.value='female'){
this.style.backgroundColor='red';
}
}
//让content 观察select 变化
sel.attach('content',content);
</script>
</body>
</html>
0 0
- 用观察者模式
- 用AOP实现观察者模式
- 用javascript实现观察者模式
- 【设计模式】用AOP实现观察者模式
- 用Delphi实现观察者模式(Observer模式)
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- Visio2002、2003、2007下载地址整理 以及 相关网址链接
- spring mongodb查询
- Tomcat7中javax.el.ELException异常信息的处理
- 最短路径问题(1)
- html编码
- 用观察者模式
- 性能测试随笔
- Conversion to Dalvik format failed with error 1 解决问题
- Java进行DESC加密方法
- WIn7-64的安装
- Activity之间传递数据 Intent extra (p86-88)
- javaweb中url问号传值,中文乱码解决办法
- vs2010 LINK fatal error LNK1123 failure 解决方法
- View到达顶部及底部边界判断