gpt4 book ai didi

javascript - JS : Keyboard input: Disable "8" from inputting when entering "* " (multiply sign)

转载 作者:行者123 更新时间:2023-12-03 07:15:45 24 4
gpt4 key购买 nike

我在使键盘输入工作时遇到了麻烦,我什至不得不将不同的键分成“mousedown”和“keydown”才能使其工作。现在最后一个问题是,当输入星号进行乘法 (*) 时,8(位于同一按钮上)也会输入。

如何解决这个问题?

我在这里的小奖励:有谁知道为什么 1.5-1.3 = 0.19999(rec)?

http://codepen.io/kreitzo/pen/RapEqp

html

<div id="calculator">
<div id="screen">
<div id="calc">0</div>
<div id="result">0</div>
</div>
<button class="value">1</button><button class="value">2</button><button class="value">3</button><button class="value">+</button><button class="value">4</button><button class="value">5</button><button class="value">6</button><button class="value">-</button><button class="value">7</button><button class="value">8</button><button class="value">9</button><button class="value">*</button><button class="value">.</button><button class="value">0</button><button class="CE"> &larr;</button><button class="value">/</button><button class="equals">=</button><button class="C">C</button>

</div>

js

$(document).ready(function() {

var string = "";

/* Calculator input string */
$(".value").mousedown(function() {
string += $(this).text();
$("#calc").text(string);
});
/* Clear all */
$(".C").click(function() {
string = "";
$("#calc, #result").text("0");
});
/* Clear last entry */
$(".CE").click(function() {
string = string.slice(0, string.length - 1);
$("#calc").text(string);
});

/* Show result */
$(".equals").click(function() {
$("#result").text(eval(string));
});

/* Enabling keyboard input */

$(document).keydown(function(event) {

/* Numbers 0 - 9 */
if (event.which == 48) {
string += 0;
$("#calc").text(string);
}
if (event.which == 49) {
string += 1;
$("#calc").text(string);
}
if (event.which == 50) {
string += 2;
$("#calc").text(string);
}
if (event.which == 51) {
string += 3;
$("#calc").text(string);
}
if (event.which == 52) {
string += 4;
$("#calc").text(string);
}
if (event.which == 53) {
string += 5;
$("#calc").text(string);
}
if (event.which == 54) {
string += 6;
$("#calc").text(string);
}
if (event.which == 55) {
string += 7;
$("#calc").text(string);
}
if (event.which == 56) {
string += 8;
$("#calc").text(string);
}
if (event.which == 57) {
string += 9;
$("#calc").text(string);
}
/* Show result with enter */
if (event.which == 13) {
$("#result").text(eval(string));
}
/* Backspace remove last entry */
if (event.which == 8) {
string = string.slice(0, string.length - 1);
$("#calc").text(string);
}
/* Clear all with escape */
if (event.which == 27) {
string = "";
$("#calc, #result").text("0");
}

});

$(document).keypress(function(event) {
/* Start of operators */
if (event.which == 43) {
string += '+';
$("#calc").text(string);
}
if (event.which == 45) {
string += "-";
$("#calc").text(string);
}
if (event.which == 42) {
string += "*";
$("#calc").text(string);
}
if (event.which == 47) {
string += "/";
$("#calc").text(string);
}
if (event.which == 46) {
string += ".";
$("#calc").text(string);
}
});

});

最佳答案

您可以确保仅当 Shift 键未按下时才添加“8”:

if (event.which == 56 && !event.shiftKey) {
string += 8;
$("#calc").text(string);
}

event.shiftKey 是一个 bool 值,让您知道事件触发时是否按下了shift。

关于javascript - JS : Keyboard input: Disable "8" from inputting when entering "* " (multiply sign),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com