恩 代码不是我写的 网上找的 我没有看懂 看了半天没看懂 我想让大神帮我看看 帮我加上注释 让一个新人能看懂
还有就是 ,有三个功能没有实现 我写了几次,写错了 呵呵 所以求帮助
还有就是怎么在源代码上 实现 1+2+3*6/7 这个公式呢 先加在乘后除 要在屏幕上显示的
完了按等于键 等于出结果 像图片显示那样 不能就算了 代码下面
你这个我就没空看了,随便写了一个,自己看注释
javascript计算器
js
jquery.
$(function(){
var countSize;
$("#buttons input.num").click(function(){
var numVal = $(this).val();
if( $("#display #down").text() == "0" ){
if( numVal == "." ){
$("#display #down").append(numVal);
}else{
$("#display #down").text(numVal);
}
}
else if( $("#display #down").text().indexOf(".") > 0 ){
if( numVal == "." ){
}else{
$("#display #down").append(numVal);
}
}else if( $("#display #down").text() == "" && numVal == "." ){
$("#display #down").text("0").append(numVal);
}
else{
$("#display #down").append(numVal);
}
})
$("#buttons input.math").click(function(){
var upNum = $("#display #up .upNum").text();
var upMath = $("#display #up .countSize").text();
var downNum = $("#display #down").text();
if( upNum.length !== 0 && downNum.length !== 0 && upMath.length !== 0 ){
countSize = $("#display #up .countSize").text();
var upNum = $("#display #up .upNum").text();
var downNum = $("#display #down").text();
if( upNum == "" || downNum == "" ){return false;}
var resultNum = count[countSize](upNum,downNum);
$("#display #up .upNum").text(resultNum);
countSize = $(this).val();
$("#display #up .countSize").text(countSize);
$("#display #down").text("");
}else{
countSize = $(this).val();
$("#display #up")
.text(downNum)
.wrapInner('<span class="upNum" /></span>')
.append('<span class="countSize">' + countSize + '</span>');
$("#display #down").text("");
}
})
$("#buttons input.equal").click(function(){
var upNum = $("#display #up .upNum").text();
var downNum = $("#display #down").text();
if( upNum == "" || downNum == "" ){return false;}
var resultNum = count[countSize](upNum,downNum);
$("#display #down").text(resultNum);
$("#display #up").text("");
})
$("#buttons input.clearDisplay").click(function(){
$("#display #up").text("");
$("#display #down").text("0");
})
$("#buttons input.pecent").click=function(){
var numVal =-numVar.text();
}
$("#buttons input.tg").click=function(){
var numVal
}
var count = {
'+': function(a,b){return accAdd(a,b);},
'-': function(a,b){return accSub(a,b);},
'*': function(a,b){return accMul(a,b);},
'/': function(a,b){return accDiv(a,b);}
}
function accAdd(arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m;
}
function accSub(arg1,arg2){
var r1,r2,m,n;
try{r1=arg2.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg1.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
n=(r1>=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
}
function accMul(arg1,arg2)
{
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}
function accDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{t1=arg1.toString().split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}
with(Math){
r1=Number(arg1.toString().replace(".",""));
r2=Number(arg2.toString().replace(".",""));
return (r1/r2)*pow(10,t2-t1);
}
}
})
html
<div id="calc">
<div id="display">
<div id="up" class="up"></div>
<div id="down" class="down">0</div>
</div>
<div id="buttons">
<div id="math">
<input type="button" value="/" class="math" id="divide" />
<input type="button" value="*" class="math" id="multiply" />
<input type="button" value="-" class="math" id="reduce" />
<input type="button" value="+" class="math" id="plus" />
</div>
<div id="num">
<input type="button" value="7" class="num" id="seven" />
<input type="button" value="8" class="num" id="eight" />
<input type="button" value="9" class="num" id="nine" />
<input type="button" value="%" class="pecent" id="pecent" />
<input type="button" value="4" class="num" id="four" />
<input type="button" value="5" class="num" id="five" />
<input type="button" value="6" class="num" id="six" />
<input type="button" value="±" class="zf" id="zf" />
<input type="button" value="1" class="num" id="one" />
<input type="button" value="2" class="num" id="two" />
<input type="button" value="3" class="num" id="three" />
<input type="button" value="←" class="tg" id="tg" />
<input type="button" value="0" class="num" id="zero" />
<input type="button" value="." class="num" id="point" />
<input type="button" value="=" class="equal" id="equal" />
<input type="button" value="C" class="clearDisplay" id="clearDisplay" />
</div>
</div>
你通过debug跟一下就能看懂了,至于那个公式,input里面输入你的公司。等号点击事件中获取到值,进行四则运算的判断运算输出就好了
参考这篇文章:http://blog.csdn.net/qq_21792169/article/details/50814700
function show(){ var date = new Date(); //日期对象 var now = ""; now = date.getFullYear()+"年"; //读英文就行了 now = now + (date.getMonth()+1)+"月"; //取月的时候取的是当前月-1如果想取当前月+1就可以了 now = now + date.getDate()+"日"; now = now + date.getHours()+"时"; now = now + date.getMinutes()+"分"; now = now + date.getSeconds()+"秒"; document.getElementById("nowDiv").innerHTML = now; //div的html是now这个字符串 setTimeout("show()",1000); //设置过1000毫秒就是1秒,调用show方法 }<!DOCTYPE html>
<font size="5" color="ff99ff">Design by:<a target="_blank" href="http://blog.csdn.net/qq_21792169/article/details/50629515" >网络人VS灰鸽子</a> 2016-3-3 邮箱:linux_drv@yeah.net</font>
<! 这里为了方便我就直接添加空格来布局了,这里可以使用style>