我不久前创建了这个 javascript 计算器。多年来它对我来说非常有效,但在回到它之后,我发现它不再简单了。
这是我的 js 小提琴... http://jsfiddle.net/AdamMartin121/XXFXv/
这是我使用的代码。它很长,可能没有必要。有谁知道首先为什么它不起作用,其次是一种更有效的方法?
Javascript Code...
var display = document.getElementById("display");
document.addEventListener("DOMContentLoaded", init, false);
function init() {
var name = prompt("Enter your name", "");
if (name == null)
{
document.getElementById("head").innerHTML="Enter a name!";
}
else
{
document.getElementById("head").innerHTML= "<p>Welcome, " +name+ ". </p> <p2> This is a simple Javascript calculator. It is open source and was designed to help people to learn Javascript. <br> Feel free to copy and paste the code, and use it how you wish. </p>";
}
document.getElementById("calc").display.style.block;
}
function naught() {
var display= document.getElementById("display");
display.value+="0";
}
function one() {
var display= document.getElementById("display");
display.value+="1"
}
function two() {
var display= document.getElementById("display");
display.value+= "2";
}
function three() {
var display= document.getElementById("display");
display.value+="3";
}
function four() {
var display= document.getElementById("display");
display.value+="4";
}
function five() {
var display= document.getElementById("display");
display.value+="5";
}
function six() {
var display= document.getElementById("display");
display.value+="6";
}
function seven() {
var display= document.getElementById("display");
display.value+="7";
}
function eight() {
var display= document.getElementById("display");
display.value+="8";
}
function nine() {
var display= document.getElementById("display");
display.value+="9";
}
function add() {
var display= document.getElementById("display");
display.value+="+";
}
function take() {
var display= document.getElementById("display");
display.value+="-";
}
function times() {
var display= document.getElementById("display");
display.value+="*";
}
function divi() {
var display= document.getElementById("display");
display.value+="/";
}
function equal() {
var display= document.getElementById("display");
display.value= eval(display.value);
}
提前致谢。