Home  >  Article  >  Web Front-end  >  jQuery implements simple online calculator method

jQuery implements simple online calculator method

小云云
小云云Original
2018-05-17 16:02:413143browse

This article mainly introduces the simple online calculator function implemented by jQuery, and analyzes the related operating skills of jQuery implementing simple four arithmetic operations in the form of a complete example. Friends who need it can refer to it. I hope it can help everyone.

Let’s take a look at the running effect diagram first:

The complete code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.php.cn/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.php.cn/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery计算器</title>
<style type="text/css"><!--
 .p{
 border:green 1px;
 border-style:solid;
 width:300px;
 text-align:center;
 }
 span{
 background-color:#CCCCCC;
 font-size:32px;
 font-family:"微软雅黑";
 }
.input{
 border: 1px solid #6666FF;
}
--></style><style type="text/css"> .p{
 border:green 1px;
 border-style:solid;
 width:300px;
 text-align:center;
 }
 span{
 background-color:#CCCCCC;
 font-size:32px;
 font-family:"微软雅黑";
 }
.input{
 border: 1px solid #6666FF;
}</style>
<script src="jquery-1.7.2.min.js" type="text/JavaScript"></script>
<script language="javascript"><!--
$(document).ready(function() {//传说中的ready
$("form p input:text").addClass("input");//找到form里面p包含的input标签类型为text的元素 jQuery强悍
var num1,num2;
$("#jia").click(function() {
num1=parseFloat($("#num1").val());
num2=parseFloat($("#num2").val());
$("#reset").val(num1+num2);
});
$("#jian").click(function() {
num1=parseFloat($("#num1").val());
num2=parseFloat($("#num2").val());
$("#reset").val(num1-num2);
});
$("#cheng").click(function() {
num1=parseFloat($("#num1").val());
num2=parseFloat($("#num2").val());
$("#reset").val(num1*num2);
});
$("#chu").click(function() {
num1=parseFloat($("#num1").val());
num2=parseFloat($("#num2").val());
$("#reset").val(num1/num2);
});
});
// --></script>
</head>
<body style="text-align:center">
<form>
<p class="p">
<p><span>jQuery简单计算器</span></p>
<p>第一个数:<input type="text" id="num1" ></p>
<p>第二个数:<input type="text" id="num2" ></p>
<p><input type="button" value=" + " id="jia"><input type="button" value=" - " id="jian"><input type="button" value=" * " id="cheng"><input type="button" value=" / " id="chu"></p>
<p>结果:<input type="text" id="reset" /></p>
</p>
</form>
</body>
</html>

Related recommendations:

Detailed explanation of sample code for PHP to implement online calculator function

Use php+mysql to make a powerful online calculator_PHP tutorial

A Simple Online Calculator

The above is the detailed content of jQuery implements simple online calculator method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn