Extjs4学习笔记-计算器

主要练习addBehaviors()函数使用:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>计算器</title>
		
		<link rel="stylesheet" type="text/css" href="../extjs/resources/css/ext-all.css" />
		<script type="text/javascript" src="../extjs/bootstrap.js"></script>
		<script type="text/javascript" src="../extjs/ext-lang-zh_CN.js"></script>
		
		<style type="text/css">
			table {
				margin: 20px 0 0 20px;
				font-size: 20px;
				line-height: 40px;
				border: 1px solid #000;
				padding: 3px;
			}
			
			th {
				text-align: center;
			}
			
			#Calculator {
				border: 1px solid #000;
			}
			
			#result {
				width: 156px;
				height: 30px;
				font-size: 16px;
				font-weight: bold;
			}
			
			.cal, .number, .op, .sign {
				width: 40px;
				height: 40px;
				text-align: center;
			}
			
			.cmd {
				width: 80px;
				height: 40px;
				text-align: center;
			}
		</style>
		
		<script type="text/javascript">
		Ext.onReady(function() {
			var cal = function() {
				switch(op) {
					case "-" :
						first = parseFloat(first) - parseFloat(second);
						break;
					case "*" :
						first = parseFloat(first) * parseFloat(second);
						break;
					case "/" :
						second = parseFloat(second);
						if(second != 0) {
							first = parseFloat(first) - parseFloat(second);
						}
						break;
					default:
						first = parseFloat(first) + parseFloat(second);
						break;
				}
				op = "";
				if(arguments.length > 0) {
					op = arguments[0];
				}
				second = "";
				result.value = first;
			}
			
			var first = "";	//用来保存第一个数字
			var second = "";	//用来保存第二个数字
			var op = "";	//用来保存运算符
			var result = Ext.getDom("result");	//指向结果文本框的DOM对象
			
			Ext.addBehaviors({
				
				"input.number@click" : function(e, el) {
					if(Ext.isEmpty(op)) {
						if(!(el.value == 0 && first == 0)) {
							first = first + el.value;
							result.value = first;
						}
					} else {
						if(!(el.value == 0 && second == 0)) {
							second = second + el.value;
							result.value = second;
						}
					}
				},
				
				"input.cmd@click" : function(e, el) {
					if(el.value == "C") {
						if(Ext.isEmpty(op)) {
							first = "";
						} else {
							second = "";
						}
						result.value = "0";
					} else {
						cal();
					}
				},
				
				"input.sign@click" : function(e, el) {
					if(el.value == ".") {
						if(Ext.isEmpty(op)) {
							if(first.toString().indexOf(".") == -1) {
								first += ".";
								result.value = first;
							}
						} else {
							if(second.toString().indexOf(".") == -1) {
								second += ".";
								result.value = second;
							}
						}
					} else {
						if(Ext.isEmpty(op)) {
							first *= -1;
							result.value = first;
						} else {
							second *= -1;
							result.value = sceond;
						}
					}
				},
				
				"input.op@click" : function(e, el) {
					if(Ext.isEmpty(op) || Ext.isEmpty(second)) {
						op = el.value;
						result.value = "0";
					} else {
						cal(el.value);
					}
				}
				
			});
		});
		</script>
		
	</head>
	<body>
		<table cellpadding="1" cellspacing="1" border="0">
			<tr style="border: 1px solid #000; background: #2159c2; color: #fff">
				<th colspan="4">计算器</th>
			</tr>
			
			<tr>
				<td colspan="4" align="center"><input id="result" readonly=true style="text-align: right" type="text" value="0" /></td>
			</tr>
			
			<tr>
				<td colspan="2"><input class="cmd" type="button" value="=" /></td>
				<td colspan="2"><input class="cmd" type="button" value="C" /></td>
			</tr>
			
			<tr>
				<td><input class="number" type="button" value="7" /></td>
				<td><input class="number" type="button" value="8" /></td>
				<td><input class="number" type="button" value="9" /></td>
				<td><input class="op" type="button" value="+" /></td>
			</tr>
			
			<tr>
				<td><input class="number" type="button" value="4" /></td>
				<td><input class="number" type="button" value="5" /></td>
				<td><input class="number" type="button" value="6" /></td>
				<td><input class="op" type="button" value="-" /></td>
			</tr>
			
			<tr>
				<td><input class="number" type="button" value="1" /></td>
				<td><input class="number" type="button" value="2" /></td>
				<td><input class="number" type="button" value="3" /></td>
				<td><input class="op" type="button" value="*" /></td>
			</tr>
			
			<tr>
				<td><input class="sign" type="button" value="-/+" /></td>
				<td><input class="number" type="button" value="0" /></td>
				<td><input class="sign" type="button" value="." /></td>
				<td><input class="op" type="button" value="/"/ /></td>
			</tr>
		</table>
	</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值