0
我有一個任務,我必須使用java腳本和HTML創建一個計算器。我已經編寫了足夠的代碼,當我使用操作符時,它將執行數學運算,但數字不會顯示在顯示框中。我也不確定如何編碼清除按鈕和取消按鈕。 這是外部的JavaScript文件。爲javascript計算器創建一個清除按鈕代碼
var mycalc = new calc(null,null,null);
function calc(firstNum,secNum,operator)
{
this.firstNum = firstNum;
this.secNum = secNum;
this.operator = operator;
}
function numberClick(ref)
{
//alert.ref.value
if(mycalc.firstNum == null)
mycalc.firstNum = ref.value
else if (mycalc.secNum == null)
mycalc.secNum = ref.value //ref.value is the value from the input
//alert("firstNum" + mycalc.operator.firstNum);
//alert("secNum") + mycalc.secNum);
}
function operatorClick(ref)
{
if (mycalc.operator == null)
mycalc.operator = ref.value;
}
function calculateResultClick()
{
if (mycalc.operator == "+")
{
var ans = parseInt(mycalc.firstNum) + parseInt(mycalc.secNum);
alert(ans);
}
if (mycalc.operator == "-")
{
var ans2 = parseInt(mycalc.firstNum) - parseInt(mycalc.secNum);
alert(ans2);
}
if (mycalc.operator == "*")
{
var ans3 = parseInt(mycalc.firstNum) * parseInt(mycalc.secNum);
alert(ans3)
}
if (mycalc.operator == "/")
{
var ans4 = parseInt(mycalc.firstNum)/parseInt(mycalc.secNum);
alert(ans4)
}
}
function Clear()
{
}
你能不能給我們的jsfiddle,好嗎? – BenM 2013-02-21 22:09:05