实现功能:通过填写数字,选择运算符,计算得出最终结果。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>简单的计算器</title>
</head>
<body>
	<div id="counter">
		<input v-model="num1" type="text">
		<select v-model="sym">
			<option value="0">+</option>
			<option value="1">-</option>
			<option value="2">*</option>
			<option value="3">/</option>
		</select>
		<input v-model="num2" type="text">
		<button v-on:click="calculate">计算</button>
		<span>{{resault}}</span>
	</div>
	<script src="./dist/vue.js"></script>
	<script>
		new Vue({
			el:'#counter',
			data:{
				num1:0,
				num2:0,
				resault:0,
				sym:'1'
			},
			methods:{
				calculate:function(){
					switch(this.sym){
						case '0':
								this.resault = parseInt(this.num1)+parseInt(this.num2);
								break;
						case '1':
								this.resault = parseInt(this.num1)-parseInt(this.num2);
								break;
						case '2':
								this.resault = parseInt(this.num1)*parseInt(this.num2);
								break;
						case '3':
								this.resault = parseInt(this.num1)/parseInt(this.num2);
								break;
					}
				}
			}
		})
	</script>
</body>
</html>
Logo

前往低代码交流专区

更多推荐