if-else 语句过多。它们可以全球化吗?
我正在尝试简化这个脚本。我有 50 个这样的 if e.keyCode
语句,因此双重嵌套 if/else 语句看起来很荒谬,但我所做的所有其他尝试都没有奏效。
第一个 if/else 语句 if(e.keyCode == 66 && e.shiftKey)
是必要的,但我不确定第二个 if (typedAdjusted >= paperWidth % charWidth)
如果相对于固定宽度在一行上输入太多字符,则会发出警告。
if (typedAdjusted >= paperWidth % charWidth)
功能可以为我提供全局功能吗?需要根据特定的密钥代码进行检查。例如,字母“B”应该被计算为 typedAdjusted,而 BACKSPACE、TAB 和 COMMAND 则不应该。
var typed = $("span.char").length;
var typedAdjusted = typed+1;
var paperWidth = 900;
var charWidth = 44;
if (e.keyCode == 66) {
if (e.keyCode == 66 && e.shiftKey) {
$('#charLine-1').append('<span class="char">B</span>');
if (typedAdjusted >= paperWidth % charWidth) {
$('body').append('<span id="warning">WARNING!</span>');
}
else {
return false;
}
}
else {
$('#charLine-1').append('<span class="char">b</span>');
if (typedAdjusted >= paperWidth % charWidth) {
$('body').append('<span id="warning">WARNING!</span>');
}
else {
return false;
}
}
}
I'm trying to streamline this script. I have 50 of these if e.keyCode
statements, so double nesting if/else statements seems ridiculous, but all other attempts I've made haven't worked.
The first if/else statement if(e.keyCode == 66 && e.shiftKey)
is necessary, but I'm not sure about the second if (typedAdjusted >= paperWidth % charWidth)
which is throwing a warning if too many characters are typed on a line relative to a fixed width.
Can the functionality if (typedAdjusted >= paperWidth % charWidth)
gives me be global? It will need to be checked against specific keyCodes. For instance, the letter "B" should be figured into typedAdjusted while BACKSPACE and TAB and COMMAND should not.
var typed = $("span.char").length;
var typedAdjusted = typed+1;
var paperWidth = 900;
var charWidth = 44;
if (e.keyCode == 66) {
if (e.keyCode == 66 && e.shiftKey) {
$('#charLine-1').append('<span class="char">B</span>');
if (typedAdjusted >= paperWidth % charWidth) {
$('body').append('<span id="warning">WARNING!</span>');
}
else {
return false;
}
}
else {
$('#charLine-1').append('<span class="char">b</span>');
if (typedAdjusted >= paperWidth % charWidth) {
$('body').append('<span id="warning">WARNING!</span>');
}
else {
return false;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说有 50 个是什么意思?你……不是说每个字母一个吗?
为什么要检查两次键码值?您是否发现代码除了字符之外完全相同?
保留一个查找表或直接字符转换,并将其缩短为单个方法:
这将创建一大堆跨度。
What do you mean by having 50 of them? You... don't mean one for each letter?
And why do you check for the keycode value twice? Do you see that the code is precisely identical except for the character?
Keep a lookup table, or direct character translation, and shorten it to a single method:
That's going to create a whole bunch of spans.
如果您喜欢检查每一项,请使用开关:根据需要传递事件调用 checkKey 函数。
要让它在整个文档上工作:
然后只需单击页面并键入字符 - 仅注意上面代码中的“b”、“c”、“d”。
IF you like checking each one use a switch: call the checkKey function as needed passing the event.
to get it to work on the entire document:
then just click on the page and type characters - note only 'b','c','d' on the code above.
如果您观察按键事件,则可以使用 String.fromCharCode(event.keyCode) 来获取输入的字符,而不必弄乱查找表。
If you observe the keypress event, you can use
String.fromCharCode(event.keyCode)
to get the character entered and not have to mess with a lookup table.