Maison  >  Article  >  interface Web  >  Comment implémenter une calculatrice simple en HTML

Comment implémenter une calculatrice simple en HTML

php中世界最好的语言
php中世界最好的语言original
2018-02-01 09:27:295808parcourir

Cette fois, je vais vous montrer comment implémenter une calculatrice simple en HTML. Quelles sont les précautions pour implémenter une calculatrice simple en HTML. Voici des cas pratiques, jetons un oeil.

<!DOCTYPE html> 
<html> 
<meta name="content-type" content="text/html; charset=UTF-8"> 
<head> 
<title>Calculator</title> 
<!--将按键内容以
字符串
形式存储在文字框中当按钮为“=”时,调用eval方法计算结果然后将结果输出文字框中--> 
<script type="text/
javascript
"> 
var numresult; 
var str; 
function 
onclick
num(nums) { 
str = 
document
.getElementById("nummessege"); 
str.value = str.value + nums; 
} 
function onclickclear() { 
str = document.getElementById("nummessege"); 
str.value = ""; 
} 
function onclickresult() { 
str = document.getElementById("nummessege"); 
numresult = eval(str.value); 
str.value = numresult; 
} 
</script> 
</head> 
<body bgcolor="affff" > 
<!--定义按键表格,每个按键对应一个事件触发--> 
<table border="1" align="center" bgColor="#bbff77" 
style="height: 350px; width: 270px"> 
<tr> 
<td colspan="4"> 
<input type="text" id="nummessege" 
style="height: 90px; width: 350px; 
font-size
: 50px" /> 
</td> 
</tr> 
<tr> 
<td> 
<input type="button" value="1" id="1" onclick="onclicknum(1)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="2" id="2" onclick="onclicknum(2)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="3" id="3" onclick="onclicknum(3)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="+" id="add" onclick="onclicknum(&#39;+&#39;)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
</tr> 
<tr> 
<td> 
<input type="button" value="4" id="4" onclick="onclicknum(4)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="5" id="5" onclick="onclicknum(5)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="6" id="6" onclick="onclicknum(6)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="-" id="sub" onclick="onclicknum(&#39;-&#39;)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
</tr> 
<tr> 
<td> 
<input type="button" value="7" id="7" onclick="onclicknum(7)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="8" id="8" onclick="onclicknum(8)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="9" id="9" onclick="onclicknum(9)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="*" id="mul" onclick="onclicknum(&#39;*&#39;)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
</tr> 
<tr> 
<td colspan="2"> 
<input type="button" value="0" id="0" onclick="onclicknum(0)" 
style="height: 70px; width: 190px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="." id="point" onclick="onclicknum(&#39;.&#39;)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
<td> 
<input type="button" value="/" id="division" 
onclick="onclicknum(&#39;/&#39;)" 
style="height: 70px; width: 90px; font-size: 35px"> 
</td> 
</tr> 
<tr> 
<td colspan="2"> 
<input type="button" value="Del" id="clear" 
onclick="onclickclear()" 
style="height: 70px; width: 190px; font-size: 35px" /> 
</td> 
<td colspan="2"> 
<input type="button" value="=" id="result" 
onclick="onclickresult()" 
style="height: 70px; width: 190px; font-size: 35px" /> 
</td> 
</tr> 
</table> 
</body> 
</html>

Je pense que vous maîtrisez les méthodes après avoir lu ces cas. Pour des informations plus intéressantes, veuillez prêter attention aux autres articles connexes sur le site Web chinois de php !

Lecture connexe :

Comment faire en sorte que le menu déroulant HTML conserve la valeur sélectionnée après la soumission et ne revienne pas à la valeur par défaut

html Comment définir le focus d'un élément

Comment contrôler les propriétés du HTML lors de l'ouverture d'une nouvelle fenêtre avec un lien hypertexte

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn