我有這個非常簡單的計算器。本質上,我想將小復活節彩蛋放入代碼的功能中。這是我的codepen以顯示我迄今爲止所做的。我已經評論過我沒有奏效的嘗試。我知道codepen不顯示警報,所以我已經嘗試過他們的實時頁面,警報仍然不起作用。這裏是JavaScript當顯示數字時附加警報()
// Variables
var appendDigits = false
var prevResult = 0
var op = "="
//Functions
function clearDisplay() {
document.calculator.display.value = 0
prevResult = 0
appendDigits = false
op = "="
} //clear
function doOp(newop)
{
var newArg =eval(document.calculator.display.value)
if (op == "+") {
prevResult = prevResult + newArg
}
else if (op == "-") {
prevResult = prevResult - newArg
}
else if (op == "/") {
prevResult = prevResult/newArg
}
else if (op == "*") {
prevResult = prevResult * newArg
}
else if (op == "=") {
prevResult = newArg
}
// else if (newArg == 42) {
// alert("You have found the meaning of life, the universe, and everything.");
// }
else {
prevResult = newArg
}
document.calculator.display.value = prevResult
appendDigits = false
op = newop
} //doOp
function digit(dig) {
if (appendDigits) {
document.calculator.display.value += dig
}
else {
document.calculator.display.value = dig
appendDigits = true
}
}
//fuction meaning(document.calculator.display.value)
//{
// if ("newArg" == "42") {
// alert("You have found the meaning of life, the universe, and everything.");
// }
// else {
// prevResult = newArg
// }
//}
我在codepen上的JS代碼的頂部添加了一個「alert('hello')」,並且警報在最新的chrome中正確彈出。 http://codepen.io/anon/pen/Kxekc – Will 2014-09-30 19:11:21
你沒有在註釋代碼中正確拼寫功能。這可能是你的問題嗎? – 2014-09-30 19:17:23