HTML代码(Calculator.html):
<!DOCTYPE html>
<html>
<head
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Calculator</title>
<link type="text/css" rel="stylesheet" href="Calculator_CSS.css">
</head>
<body>
<input type ="text" name="number1" style="width:50px;" id="Number1">
<select id="DropdownMenu1">
<option>+</option>
<option>-</option>
<option>×</option>
<option>÷</option>
</select>
<input type ="text" name="number2" style="width:50px;" id="Number2">
<input type="button" value="Calculate" id="submitButton" />
<p>The answer is: <span id="answer"></span></p>
<script src="Calculator_JS.js>"></script>
</body>
</html>
这是JavaScript代码(Calculator_JS.js):
var button = document.getElementById("submitButton");
button.onclick = function(){
if (document.getElementById("DropdownMenu1").value == "+"){
var num1 = document.getElementById('Number1').value;
var num2 = document.getElementById('Number2').value;
var answer = num1 + num2;
document.getElementById('Answer').value = answer;
}
}
出于某种原因,当我点击计算按钮时它没有做任何事情。任何帮助都会很棒!谢谢!
答案 0 :(得分:1)
试用此代码
<html>
<body>
<script type="text/javascript">
function Calc(){
var num1a = document.getElementById("num1");
var num2a = document.getElementById("num2");
var ans = (num1a + num2a)
document.write("<b>The Answer Is:</b>" + ans)
}
</script>
<input type ="text" id="num1" style="width:50px;" id="Number1">
<input type ="text" id="num2" style="width:50px;" id="Number2">
<input type="button" value="Calculate" id="submitButton"
onClick="Calc()">
</body>
</html>
(输入2个数字,然后输入答案(x + y))问题是它将其键入字符串: 在“第一个文本框”中,“数字”为5,在第二个“5”中,当按下“计算”按钮时,它将显示
答案是: 55而不是10 ......
所以你需要做的是:
ParseInt(document.getElementById("num1").value);
ParseInt(document.getElementById("num2").value);
而不是
document.getElementById("num1");
document.getElementById("num2");
var num1a和num2a中的。 这将解决问题 :)
答案 1 :(得分:0)
请参阅this fiddle获取答案。
document.getElementById('Answer').value = answer;
document.getElementById('Answer').innerHTML = answer;
醇>
答案 2 :(得分:0)
我发现了两个错误。
document.getElementById('Answer')
中的资本 A 。应该是document.getElementById('answer')
。由于id的名称也很小a。.value = answer
它应该是.textContent = answer
或innerText / innerHtml。答案 3 :(得分:0)
非常简单的计算器(html5 css3 js)
JS
function calc(e){
var a=e.target.innerText,b=this.firstChild;
b.value=a=='='?eval(b.value):a=='C'?'':b.value+a;
}
var gui=document.createElement('div');
gui.className='clc';
gui.innerHTML='<input><div>'+('789+456-123*C0./='.split('').join('</div><div>'))+'</div>';
gui.addEventListener('click',calc,false);
window.onload=function(){
document.body.appendChild(gui);
}
CSS3
body,html{
height:100%;
margin:0;
padding:0;
background-color:#666;
}
*{
box-sizing:border-box;
}
.clc{
width:256px;
height:320px;
clear:both;
}
.clc>*{
border:4px solid #666;
border-radius:4px;
font:normal normal bold 20px/56px Arial,Helvetica,sans-serif;
border-radius:10px;
}
.clc>input{
width:100%;
height:20%;
text-align:right;
padding:0 20px;
}
.clc>div{
width:25%;
height:20%;
background-color:#5a5a5a;
color:#fff;
text-align:center;
float:left;
}
.clc>div:nth-child(4n+1){
background-color:#f36573;
}
.clc>div:nth-last-child(1){
width:100%;
background-color:#ffb343;
}
例如