<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>js:命令模式</title>
<script type="text/javascript" src="command.js"></script>
</head>
<style type="text/css">
button{
margin: 5px;
border: 0;
width: 70px;
height: 35px;
background: #6B78BF;
color: white;
font-size: 14px;
font-family: "微软雅黑";
cursor: pointer;
}
#textarea{
margin: 5px;
width: 400px;
height: 200px;
resize: none;
color: #666;
font-size: 14px;
border: 2px solid #6B78BF;
}
</style>
<body>
<button id="button1">刷新</button>
<button id="button2">新增</button>
<button id="button3">删除</button>
<br/>
<textarea id="textarea">
这是预设的内容
</textarea>
</body>
</html>
// 在页面中使用例:setCommand( button1, refreshMenuBarCommand );来发送命令
// setCommand 函数负责往按钮上面安装命令,预留好安装命令的接口
var setCommand = function( button , command ){
button.onclick = function(){
command.execute();
}
}
// 编写点击按钮之后的具体行为:刷新菜单界面、增加子菜单和删除子菜单
var MenuBar = {
refresh: function(){
var cur_date = new Date();
document.getElementById("textarea").value+=cur_date.toLocaleString()+" 刷新菜单目录\r";
}
}
var SubMenu = {
add: function(){
var cur_date = new Date();
document.getElementById("textarea").value+=cur_date.toLocaleString()+" 新增菜单目录\r";
},
del: function(){
var cur_date = new Date();
document.getElementById("textarea").value+=cur_date.toLocaleString()+" 删除子菜单\r";
}
}
//封装行为在命令类中
var RefreshMenuBarCommand = function( receiver ){
this.receiver = receiver;
}
RefreshMenuBarCommand.prototype.execute = function(){
this.receiver.refresh();
}
var AddSubMenuCommand = function( receiver ){
this.receiver = receiver;
}
AddSubMenuCommand.prototype.execute = function(){
this.receiver.add();
}
var DelSubMenuCommand = function( receiver ){
this.receiver =receiver
}
DelSubMenuCommand.prototype.execute = function(){
this.receiver.del();
}
//命令接收者传入到 command 对象
var refreshMenuBarCommand = new RefreshMenuBarCommand( MenuBar );
var addSubMenuCommand = new AddSubMenuCommand( SubMenu );
var delSubMenuCommand = new DelSubMenuCommand( SubMenu );
window.onload = function(){
//把 command 对象安装到 button 上面
var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");
var button3 = document.getElementById("button3");
setCommand( button1, refreshMenuBarCommand );
setCommand( button2, addSubMenuCommand );
setCommand( button3, delSubMenuCommand );
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有