HTML和JavaScript中的简单计算器

时间:2014-11-24 18:08:07

标签: javascript html

我必须用HTML编写一个计算器。我真的找不到出错的地方,也没有显示出结果。我找不到有什么问题你能帮忙吗?我在Chrome中运行它。

JavaScript文件和HTML:

showresult(choise){
var n1=parsefloat(document.getElementById('num1').value);
var n2=parsefloat(document.getElementById('num2').value);
var r;
var c=choise;

switch(c)
	{
	case '1':
		r=n1+n2;
		break;
	case '2':
		r=n1-n2;
		break;
	case '3':
		r=n1*n2;
		break;
	case '4': 
		r=n1/n2;
		break;
	case '5':
		r=n2*100/n1;
		break;
	default:
		break;
			
	}
document.getElementById('result').innerHTML=r;

	

}
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<script src="calculator.js" type="text/javascript"></script>
</head>
<body>


<h1>My calculator</h1>
<table border="1" cellpadding="5" cellspacing="5" width="600">
<tr align="center">
<td>First number</td>
<td>Second Number</td>
<td>Result</td>
</tr>
<tr align="center">
<td><input name="number1" type="text" size=10 id='num1'/></td>
<td><input name="number2" type="text" size=10 id='num2'/></td>
<td> <input type="text" id='result' readonly ></td>
</tr>
<tr>
<td colspan="3">
<button onclick="showresult('1')">+</button>
<button onclick="showresult('2')">-</button>
<button onclick="showresult('3')">*</button>
<button onclick="showresult('4')">/</button>
<button onclick="showresult('5')">%</button>
</td>
</tr>

</table>
</body>
</html>

7 个答案:

答案 0 :(得分:1)

要解决的问题: 1)在引用value元素时,分配给innerHTML,而不是input。 (他们没有内容,因此没有innerHTML。 2)使用关键字function启动函数声明。 3)它是parseFloat,而不是parsefloat。 JavaScript区分大小写。

最小化代码:

function showresult(choise){
var n1=parseFloat(document.getElementById('num1').value);
var n2=parseFloat(document.getElementById('num2').value);
var r;
var c=choise;

switch(c)
	{
	case '1':
		r=n1+n2;
		break;
	case '2':
		r=n1-n2;
		break;
	case '3':
		r=n1*n2;
		break;
	case '4': 
		r=n1/n2;
		break;
	case '5':
		r=n2*100/n1;
		break;
	default:
		break;
			
	}
document.getElementById('result').value=r;

	

}
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<script src="calculator.js" type="text/javascript"></script>
</head>
<body>


<h1>My calculator</h1>
<table border="1" cellpadding="5" cellspacing="5" width="600">
<tr align="center">
<td>First number</td>
<td>Second Number</td>
<td>Result</td>
</tr>
<tr align="center">
<td><input name="number1" type="text" size=10 id='num1'/></td>
<td><input name="number2" type="text" size=10 id='num2'/></td>
<td> <input type="text" id='result' readonly ></td>
</tr>
<tr>
<td colspan="3">
<button onclick="showresult('1')">+</button>
<button onclick="showresult('2')">-</button>
<button onclick="showresult('3')">*</button>
<button onclick="showresult('4')">/</button>
<button onclick="showresult('5')">%</button>
</td>
</tr>

</table>
</body>
</html>

答案 1 :(得分:0)

确保正确声明该功能。

function showresult(choise) {
/* ... */
}

另外,请记住Javascript区分大小写,因此解析float的正确方法是 parseFloat() 最后,正如gillesc所指出的,在输入元素上使用属性valuedocument.getElementById('result').value=r;

答案 2 :(得分:0)

您应该在代码中将“ innerHTML”替换为“value”

如果要访问非输入HTML元素中的文本,那么您将不得不使用innerHTML属性而不是值

现在代码变为:

document.getElementById('result').value=r;

这里我们使用了value属性,所有输入元素都必须使用它来获取用户输入的值。

答案 3 :(得分:0)

您可以在代码中使用此功能

function calc(form) {
      var D = "0";
      var A = document.getElementById("num1").value;
      var B = document.getElementById("op").value;
      var C = document.getElementById("num2").value;

      switch(B){
          case'+':
          D= parseInt(A)+parseInt(C); break;
          case'-':
          D= parseInt(A)-parseInt(C); break;
          case'*':
          D=parseInt(A)*parseInt(C); break;
          case'/':
          D=parseInt(A)/parseInt(C); break;
      }
}

答案 4 :(得分:0)

<script>

	var num1;
	var op;
	
	function pressBtn(value){
		var prev = document.forms["calc"]["display"].value;
		if (!(prev == "" && value==0)){
			var newval = prev+value;
			document.forms["calc"]["display"].value = newval;
		}
		 
	}
	
	function pressOP(operator){
		op = operator;
		var display = document.forms["calc"]["display"].value;
		num1 = parseInt(display);
		document.forms["calc"]["display"].value = "";
	}
	
	function pressEq(){
		var display = document.forms["calc"]["display"].value;
		var num2 = parseInt(display);
		switch(op){
			case "P":
				var ans=num1+num2;
				break;
			case "M":
				var ans=num1-num2;
				break;	
		}
		document.forms["calc"]["display"].value = ans;
	}
</script>
<style>
	input[type=button]{
		width: 100px;
		height: 100px;
		font-size: 36px;
		margin: 5px;
	}	
	input[type=text]{
		width: 300px;
		height: 75px;
		font-size: 48px;
		margin: 5px;
		text-align: right;
	}
</style>
<body style="background: #ccc;">
	<div style="width: 500px; margin: auto; background: #fff; text-align: center">
		<form name="calc">
			<input type="text" name="display" readonly ><br>
			<input type="button" name="btn7" value="7" onClick="pressBtn(7)">
			<input type="button" name="btn8" value="8" onClick="pressBtn(8)">
			<input type="button" name="btn9" value="9" onClick="pressBtn(9)"><br>
			<input type="button" name="btn4" value="4" onClick="pressBtn(4)">
			<input type="button" name="btn5" value="5" onClick="pressBtn(5)">
			<input type="button" name="btn6" value="6" onClick="pressBtn(6)"><br>
			<input type="button" name="btn1" value="1" onClick="pressBtn(1)">
			<input type="button" name="btn2" value="2" onClick="pressBtn(2)">
			<input type="button" name="btn3" value="3" onClick="pressBtn(3)"><br>
			<input type="button" name="btn0" value="0" onClick="pressBtn(0)">
            <input type="button" name="btnPlus" value="+" onClick="pressOP('P')" >
            <input type="button" name="btnPlus" value="-" onClick="pressOP('M')" ><br>
            <input type="button" name="btnEq" value="=" onClick="pressEq();" >
		</form>
	</div>
</body>

答案 5 :(得分:0)

var cnum = document.getElementsByClassName('c-num')[0];
var cope = document.getElementsByClassName('c-operator')[0];
var intom = document.getElementById('intop');
var inbottom = document.getElementById('inbottom');
var newinbottom;
var newinbottom2;
var sign;
for(i=0;i<10;i++){
	cnum.innerHTML +='<span class="numb" value="'+i+'" id="numid'+i+'" onclick="puton('+i+')">'+i+'</span>';
}
document.getElementById('numid0').style.order = "1";
function puton(numb){
		inbottom.value += numb	
}
function ans(answer){
	//console.log(sign)
		if(inbottom.value ==''){
		   alert('type input');
		   }else if(sign == "plus"){
		newinbottom2 = inbottom.value;
		intom.value = inbottom.value;
			inbottom.value = parseInt(newinbottom) + parseInt(newinbottom2);   
	}else if(sign == "minus"){
			 newinbottom2 = inbottom.value;
		intom.value = inbottom.value;
			inbottom.value = newinbottom - newinbottom2;
			 }else if(sign == "divi"){
				newinbottom2 = inbottom.value;
		intom.value = inbottom.value;
			inbottom.value = newinbottom / newinbottom2;
			 }else if(sign == "mul"){
				newinbottom2 = inbottom.value;
		intom.value = inbottom.value;
			inbottom.value = newinbottom * newinbottom2;
			 }
			
			sign ='';
	
}
function opr(opra){
	if(sign != '' && sign != undefined){
		ans();
	}
	else if(opra == "clr"){
			 	intom.value =''
			 	inbottom.value ='';
				newinbottom='';
				newinbottom='';
			 	sign ='';
			 }
	else if(opra == "plus"){
	newinbottom = inbottom.value;
	intom.value = inbottom.value;
	inbottom.value = '';
		sign = "plus";
	}else if(opra == "minus"){
	newinbottom = inbottom.value;
	intom.value = inbottom.value;
	inbottom.value = '';
			 sign = "minus";
			 }else if(opra == "divi"){
	 newinbottom = inbottom.value;
	intom.value = inbottom.value;
	inbottom.value = '';
				sign = "divi";
			 }else if(opra == "mul"){
 	newinbottom = inbottom.value;
	intom.value = inbottom.value;
	inbottom.value = '';
				sign = "mul";
			 }
}
	
.main{
	width:100vw;
	height: 100vh;
	display: flex;
    justify-content: center;
	}
	.c-body {
    width: 400px;
    height: 255px;
    background: #ccc;
    border: 1px solid;
	}
	.c-num{
		width: 100%;
		display: flex;
		flex-wrap: wrap;
	}
	.c-operator{
		flex-shrink: 1;
		display: flex;
		flex-wrap: wrap;
		flex-direction: column;
	}
	.c-row{
		display: flex;
  		flex-flow: row;
	}
	.c-operator [id^='numid']{
	width: 90px;
	flex-direction: initial;
	}
	[id^='numid'] {
    display: flex;
    flex-shrink: 1;
    background: #fff;
    padding: 4px;
    margin: 4px;
    width: 80px;
    justify-content: center;
    cursor: pointer;
	flex-direction: inherit;
}
	.c-minputs .inbox{
		display: block;
		width: 100%;
	}
	[id^='numid']:hover{
		background: #f5f5f5;
	}
<div class="main">
		<div class="c-body">
			<div class="c-minputs">
				<input id="intop" type="number" class="topin inbox" disabled>
			<input id="inbottom" type="number" class="bottomin inbox" placeholder="Type Here">
	</div>
	<div class="c-row">
	<div class="c-num">
			
			</div>
			<div class="c-operator">
		<span class="numb" id="numidplus" onclick="opr('plus')">+</span>
		<span class="numb" id="numidminus" onclick="opr('minus')">-</span>
		<span class="numb" id="numiddiv" onclick="opr('divi')">/</span>
		<span class="numb" id="numidmul" onclick="opr('mul')">*</span>
		<span class="numb" id="numidans" onclick="ans('answer')">=</span>
		<span class="numb" id="numidclr" onclick="opr('clr')">c</span>
		</div>
	</div>
		</div>
	</div>

答案 6 :(得分:0)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>stackoverflow</title>

    <style>
        body{
            background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
        }
    .button {
        box-shadow: 0px 10px 14px -7px #276873;
        background: linear-gradient(to bottom, #599bb3 5%, #408c99 100%);
        background-color: #599bb3;
        border-radius: 4px;
        border: 1px solid #29668f;
        display: inline-block;
        cursor: pointer;
        color: #ffffff;
        font-family: Arial;
        font-size: 25px;
        font-weight: bold;
        padding: 21px 6px;
        text-decoration: none;
        text-shadow: 0px 1px 0px #3d768a;
        height: 50px;
        width: 80px;
        padding: 0px;
    }
    .button:hover {
        background:linear-gradient(to bottom, #408c99 5%, #599bb3 100%);
        background-color:#408c99;
    }
    form>input{
        height: 40px;
        width: 329px;
        border-radius: 4px;
        border: none;
        margin-left: 500px;
        font-size: 30px;
    }
    table{
        margin-left: 500px;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    }
    </style>

</head>
<body>

    <div id="main">

        <form name="form">
            <input name="textview"> 
        </form>
            <table>
                <tr>
                    <td><input type="button" class="button" value="Clear" onClick="c()"></td>
                </tr>
                <tr>
                    <td><input type="button" class="button" value="7" onClick="insert(7)"></td>
                    <td><input type="button" class="button" value="8" onClick="insert(8)"></td>
                    <td><input type="button" class="button" value="9" onClick="insert(9)"></td>
                    <td><input type="button" class="button" value="/" onClick="insert('/')"></td>

                </tr>
                <tr>
                    <td><input type="button" class="button" value="4" onClick="insert(4)"></td>
                    <td><input type="button" class="button" value="5" onClick="insert(5)"></td>
                    <td><input type="button" class="button" value="6" onClick="insert(6)"></td>
                    <td><input type="button" class="button" value="-" onClick="insert('-')"></td>

                </tr>
                <tr>
                    <td><input type="button" class="button" value="1" onClick="insert(1)"></td>
                    <td><input type="button" class="button" value="2" onClick="insert(2)"></td>
                    <td><input type="button" class="button" value="3" onClick="insert(3)"></td>
                    <td><input type="button" class="button" value="+" onClick="insert('+')"></td>

                </tr>
                <tr>
                    <td><input type="button" class="button" value="0" onClick="insert(0)"></td>
                    <td><input type="button" class="button" value="." onClick="insert('.')"></td>
                    <td><input type="button" class="button" value="=" onClick="equal('=')"></td>
                    <td><input type="button" class="button" value="*" onClick="insert('*')"></td>

                </tr>
            </table>


    </div>

    <script>
        function insert(num){
            document.form.textview.value = document.form.textview.value+num;
        }
        function equal(){
            let exp = document.form.textview.value;
            if(exp){
                document.form.textview.value = eval(exp)
            }
            else{
                console.log('Something Wrong')
            }
        }
        function c(){
            document.form.textview.value = null;
        }
    </script>

</body>
</html>