JavaScript development calculator JS code
HTML was introduced earlier, now we mainly talk about how to write JS code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<title>计算器</title>
<script type="text/javascript" src=""></script>
<script>
//点击按钮执行操作
var resultDom = document.getElementById("result"); //var 是定义变量,
var operate = true; //定义一个变量,防止运算符的连续操作
var xop = true; //定义一个变量,防止小数点可以连续输出
function command(num){ //function 是封装command
if(needclear==1){
needclear=0;
resultDom.value='';
}
var str = resultDom.value;
str =(str =="0"?"":str);
resultDom.value = str+num;
operate = true;
play(num);
}
//清空
function clearzero(m){
resultDom.value = 0;
play(m)
}
//计算等号
var needclear=0;
function equal(m){
needclear=1;
var result = resultDom.value.toString();
var r = eval(result);
resultDom.value =r;
play(m);
}
//小数点
function dot(m){
if(xop){ //对变量进行判断输出
var num = resultDom.value.toString();
num +=".";
resultDom.value = num;
xop = ture;
}
play(m);
}
//点击操作符
function tools(op,m){
if(operate){ //对运算符进行判断输出
var num = resultDom.value;
num = (num =="0"?"":num);
resultDom.value = num+op;
operate = false;
}
play(m);
}
//按键声音
function play(num){
var audioDom = document.getElementById("audio");
audioDom.innerHTML = "<embed src='wav/"+num+".wav' width='0' height='0'></embed>" //wav是声音素材的文件夹名称。
}
</script>
</body>
</html>Each function will have comments. Since the voice material cannot be uploaded, you only need to know how to write it. You can download the voice material online and try it yourself.
new file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<title>计算器</title>
<script type="text/javascript" src=""></script>
<script>
//点击按钮执行操作
var resultDom = document.getElementById("result"); //var 是定义变量,
var operate = true;
var xop = true;
function command(num){
if(needclear==1){
needclear=0;
resultDom.value='';
}
var str = resultDom.value;
str =(str =="0"?"":str);
resultDom.value = str+num;
operate = true;
play(num);
}
//清空
function clearzero(m){
resultDom.value = 0;
play(m)
}
//计算等号
var needclear=0;
function equal(m){
needclear=1;
var result = resultDom.value.toString();
var r = eval(result);
resultDom.value =r;
play(m);
}
//小数点
function dot(m){
if(xop){
var num = resultDom.value.toString();
num +=".";
resultDom.value = num;
xop = ture;
}
play(m);
}
//点击操作符
function tools(op,m){
if(operate){
var num = resultDom.value;
num = (num =="0"?"":num);
resultDom.value = num+op;
operate = false;
}
play(m);
}
//按键声音
function play(num){
var audioDom = document.getElementById("audio");
audioDom.innerHTML = "<embed src='wav/"+num+".wav' width='0' height='0'></embed>"
}
</script>
</body>
</html>
Preview
Clear
- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
Let's briefly talk about starting a business in PHP
Quick introduction to web front-end development
Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
Login verification and classic message board
Computer network knowledge collection
Quick Start Node.JS Full Version
The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
















