const bt = document.querySelectorAll('.keyboard button') const close = document.querySelector('.close') const res = document.querySelector('.res') //當(dāng)點(diǎn)擊的數(shù)字的時(shí)候 let k = 0 let one let two function arr(num) { bt[num].onclick = function () { res.innerText += bt[num].innerText res.innerText = parseFloat(res.innerText) // console.log(one) } } //小數(shù)點(diǎn) //保留結(jié)果小數(shù) function fn() { if (res.innerText.length > 8) { res.innerText = res.innerText.slice(0, 10) } if (res.innerText == 'NaN') { res.innerText = 0 } } //當(dāng)點(diǎn)擊的是運(yùn)算符號的時(shí)候 function symbol(str, fu) { bt[str].onclick = function () { k++ if (k > 1) { return } one = parseFloat(res.innerText) // switch (fu) { // case '+': // one += one // break; // case '-': // one -= one // break; // case '*': // one *= one // break; // case '/': // one /= one // break; // } res.innerText = '' close.style.display = 'block' close.innerText = bt[str].innerText console.log(one) } } arr(21) arr(18) arr(17) arr(16) arr(14) arr(13) arr(12) arr(10) arr(9) arr(8) arr(22) //運(yùn)算符號 symbol(0) symbol(7, '/') symbol(11, '*') symbol(15, '-') symbol(19, '+') console.log(bt[22].innerText) bt[22].onclick = function () { res.innerText += bt[22].innerText console.log(565) } bt[23].onclick = function () { two = parseFloat(res.innerText) switch (close.innerText) { case '%': //toFixed(11)保留11位小數(shù) res.innerText = one % two k = 0 break; case '+': res.innerText = one + two k = 0 break; case '-': res.innerText = one - two k = 0 break; case 'x': res.innerText = one * two k = 0 break; case '÷': res.innerText = one / two k = 0 break; } // console.log(res.innerText.length) fn() } bt[1].onclick = function () { res.innerText = '' } bt[2].onclick = function () { res.innerText = '0' close.innerText = 'x' close.style.display = '' one = 0 two = 0 } bt[3].onclick = function () { res.innerText = res.innerText.slice(0, res.innerText.length - 1) if (res.innerText.length === 0) { res.innerText = '0' return } } bt[4].onclick = function () { res.innerText = 1 / parseFloat(res.innerText) fn() } bt[5].onclick = function () { res.innerText = parseFloat(res.innerText) * parseFloat(res.innerText) fn() } bt[6].onclick = function () { res.innerText = Math.sqrt(parseFloat(res.innerText)) fn() } bt[20].onclick = function () { res.innerText = 0 - parseFloat(res.innerText) fn() } |