Efek akhir adalah seperti yang ditunjukkan pada Gambar 2. Ada bug: hasil mengklik % setelah bilangan bulat benar. Jika % diklik setelah desimal, hasilnya akan salah! Sisanya normal, tolong beri saya beberapa saran: Nilai input adalah tipe string. Bagaimana cara menangani bagian IF dengan benar dalam Gambar-1 di bawah ini di JS? ?
Gambar-1
Gambar-2
Kode HTML adalah sebagai berikut
<body> <v ID = "Calculator"> <div> <span> Kalkulator Sederhana </span> <span> @Walker </span> </div> <div id = "shuru"> <!-bilah input layar-> <v> <input type = "Text" id = "screenname" nama = "screenname"> </Div> </Div> div> div> input = "screenname" name = "screenname"> </Div> </Div> </div> input = "screenname" name = "screenname"> </Div> </Div> </Div> div> input = "screenname" name = "screenname"> </Div> </Div> </Div> type = "Tombol" id = "7" ontClick = "jsq (this.id)" value = "7"> <input type = "Tombol" id = "8" onclick = "jsq (this.id)" value = "8"> <input type = "ID =" 9 "OnClick =" JSQ (ini (value) <"9"> 9 ">" "" "" "" "" "" "" "" "" "" ONCUCT = "JSQ (this.id) <thate" nilai "9" OnClick = "tuige ()" value = "back"> <input type = "button" id = "c" ontClick = "clearnum ()" value = "c" style = "margin-right: 0px"> <!-baris kedua-> <input type = "tombol =" 4 "Onclick =" JSQ (this. OnClick = "jsq (this.id)" value = "5"> <input type = "Tombol" id = "6" ontClick = "jsq (this.id)" value = "6"> <input type = "Tombol" id = "*" ontClick = "jsq (ini. style = "margin-right: 0px"> <!-baris ketiga-> <input type = "tombol" id = "1" ontClick = "jsq (this.id)" value = "1"> <input type = "Tombol" id = "2" ontClick = "jsq (this.id)" value = "2"> <input type = "" "3" 3 value="3"> <input type="button" id="+" onclick="jsq(this.id)" value="+"> <input type="button" id="-" onclick="jsq(this.id)" value="-" style="margin-right:0px"> <!--Fourth row--> <input type="button" id="0" OnClick = "jsq (this.id)" value = "0"> <input type = "tombol" id = "00" ontClick = "jsq (this.id)" value = "00"> <input type = "tombol" id = ".". onclick="jsq(this.id)" value="."> <input type="button" id="%" onclick="jsq(this.id)" value="%"> <input type="button" id="eva" onclick="eva()" value="=" style="margin-right:0px"> </div> <div> <span>Welcome to JavaScript calculator</span> <span> <a href = "#" target = "_ blank"> umpan balik </a> </span> </div> </div> </body>
Kode CSS adalah sebagai berikut:
<tyle> /*Basic Reset* /*{margin: 0; Padding: 0; Ukuran kotak: kotak perbatasan; Font: 14px Arial, sans-serif;} html {tinggi: 100%; latar belakang-warna: lightslegat;}#calculator {margin: 15px auto; Lebar: 330px; Tinggi: 400px; Border: 1px Solid Lightgray; Latar Belakang: Darkgrey; padding: 15px;}/*logo*/. Logo {Height: 20px;}. Logo .name {float: left; line-height: 30px;}. Logo .verson {float: kanan; Line-height: 30px;}/*Screen*/#shuru {margin-top: 15px;}. Layar {margin-top: 5px; Lebar: 300px; Tinggi: 40px; Teks-Align: Benar; Padding-Right: 10px; font-size: 20px;}#keys {border: 1px solid lightgray; Tinggi: 223px; margin-top: 25px; padding: 8px;}#keys .last {margin-right: 0px;}. footer {margin-top: 20px; tinggi: 20px;}. footer .link {float: right;}#keys .buttons {float: left; Lebar: 42px; Tinggi: 36px; Teks-Align: tengah; Latar Belakang: Lightgray; Margin: 0 17px 20px 0;} </tyle>Kode JavaScript adalah sebagai berikut:
<script> var num = 0; // Tentukan data fungsi input pertama JSQ (num) {// Dapatkan input saat ini if (num == "%") {document.geteLementById ('screenname'). Value = math.round (document.geteLementById ('screenname'). Value)/100; } else {document.geteLementById ('screenname'). value += document.geteLementById (num) .value; }} function eva () {// Hitung dokumen hasil input.getElementById ("screenname"). value = eval (document.geteLementById ("screenname"). value); } fungsi clearnum () {// clear 0 document.geteLementById ("screenname"). value = null; document.geteLementById ("screenname"). focus (); } function tuige () {// backspace var arr = document.geteLementById ("screenname"); arr.value = arr.value.substring (0, arr.value.length - 1); } </script>Artikel di atas JavaScript mengimplementasikan efek kalkulator sederhana [yang disarankan] adalah semua konten yang saya bagikan dengan Anda. Saya harap Anda dapat memberi Anda referensi dan saya harap Anda dapat mendukung wulin.com lebih lanjut.