This article example shares the specific code of Vue to implement a simple calculator for your reference. The specific content is as follows Function Introduction 1. Addition, subtraction, multiplication and division can be realized The renderings are average, the style is random, mainly focusing on the function and implementation method Code and explanation 1. HTML part First, lay out the layout, write out what you want to do, and bind a click event to each button <div id="box"> <table> <tr> <td><input type="button"value="del" @click="del()"></td> <td><input type="button"value="C" @click="clean()"></td> <td colspan="2"><input type="text" style="width: 200px" value="" v-model="rel"></td> </tr> <tr> <td><input type="button"value="7" @click="add('7')"></td> <td><input type="button"value="8" @click="add('8')"></td> <td><input type="button"value="9" @click="add('9')"></td> <td><input type="button"value="/" @click="add('/')"></td> </tr> <tr> <td><input type="button"value="4" @click="add('4')"></td> <td><input type="button"value="5" @click="add('5')"></td> <td><input type="button"value="6" @click="add('6')"></td> <td><input type="button"value="*" @click="add('*')"></td> </tr> <tr> <td><input type="button"value="1" @click="add('1')"></td> <td><input type="button"value="2" @click="add('2')"></td> <td><input type="button"value="3" @click="add('3')"></td> <td><input type="button"value="-" @click="add('-')"></td> </tr> <tr> <td><input type="button"value="0" @click="add('0')"></td> <td><input type="button"value="." @click="add('.')"></td> <td><input type="button"value="+" @click="add('+')"></td> <td><input type="button" value="=" v-on:click="result()" ></td> </tr> </table> </div> 2. CSS part, just write the style casually, it is not very important input{ width: 100px; height: 100px; border: 1px solid black; line-height: 100px; text-align: center; border-radius: 10px; background-color: gainsboro; outline: none; } table{ background-color: #b3d7ff; margin: auto; } 3. Finally, the part about vm instance var vm = new Vue({ el:"#box", data:{ rel:"", }, methods:{ add(index){//Here is the key binding method, concatenate the obtained value to the rel string this.rel +=index; }, result(){ this.rel = eval(this.rel);//Here is a calculation using the eval method this.rel = String(this.rel);//The purpose here is to display the number or string in the display bar. Only strings can be backspaced and reset to zero}, del(){//This is the backspace operation. Use the substring method of the string to intercept. Each interception starts from the 0th one and goes to the one before the length, which is equivalent to backspace. this.rel = this.rel.substring(0,this.rel.length-1); }, clean(){//This is the method of zeroing. The zeroing operation is achieved by assigning an empty string to the result. Of course, you can also use the deletion method, such as the unshift method or the pop method. It is easier to directly assign a value of empty. this.rel = ""; } } }) The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Use PSSH to batch manage Linux servers
>>: MySQL 5.6.28 installation and configuration tutorial under Linux (Ubuntu)
This article records the detailed process of down...
Table of contents 1. Download MySQL msi version 2...
Overflow Hide It means hiding text or image infor...
This article takes the deployment of Spring boot ...
1. Change the Host field value of a record in the...
Description of the phenomenon: The project uses s...
Because the Raspberry Pi is based on ARM architec...
Download the redis image docker pull yyyyttttwwww...
Compared with Windows system, Linux system provid...
01. Command Overview The tr command can replace, ...
1. Embed is illegal The <embed> tag is a pri...
Table of contents 1. Easy to read code 1. Unified...
The elements in an HTML document are arranged one...
1. Demand The base has 300 new servers, and needs...
If you set the table-layer:fixed style for a tabl...