if-else 语句过多。它们可以全球化吗?

发布于 2025-01-04 15:10:46 字数 1239 浏览 3 评论 0原文

我正在尝试简化这个脚本。我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧笙踏歌 2025-01-11 15:10:46

你说有 50 个是什么意思?你……不是说每个字母一个吗?

为什么要检查两次键码值?您是否发现代码除了字符之外完全相同?

保留一个查找表或直接字符转换,并将其缩短为单个方法:

var c = lookup(e.keyCode, e.shiftKey);
$('#charLine-1').append('<span class="char">' + c + '</span>');
if (typedAdjusted  >= paperWidth % charWidth) {
    $('body').append('<span id="warning">WARNING!</span>');
} else {
    return false;
}

这将创建一大堆跨度。


var normal = {
  66: 'b', 67: 'c', // etc.
};

var shifted = {
  66: 'B', 67: 'C', // etc.
};

/** 
 * Looks up keycode using appropriate map.
 *
 * Returns `undefined` if not found; shouldn't insert.
 */
function lookup(code, shift) {
  return shift ? shifted[code] : normal[code];
}

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:

var c = lookup(e.keyCode, e.shiftKey);
$('#charLine-1').append('<span class="char">' + c + '</span>');
if (typedAdjusted  >= paperWidth % charWidth) {
    $('body').append('<span id="warning">WARNING!</span>');
} else {
    return false;
}

That's going to create a whole bunch of spans.


var normal = {
  66: 'b', 67: 'c', // etc.
};

var shifted = {
  66: 'B', 67: 'C', // etc.
};

/** 
 * Looks up keycode using appropriate map.
 *
 * Returns `undefined` if not found; shouldn't insert.
 */
function lookup(code, shift) {
  return shift ? shifted[code] : normal[code];
}
绅刃 2025-01-11 15:10:46

如果您喜欢检查每一项,请使用开关:根据需要传递事件调用 checkKey 函数。

function checklen() {
    var typed = $("span.char").length;
    var typedAdjusted = typed + 1;
    var paperWidth = 900;
    var charWidth = 44;
    return (typedAdjusted >= paperWidth % charWidth);
}

function checkKey(e) {
    var mychar = '';
    var checkit = false;
    switch (e.keyCode) {
    case 66:
        mychar = e.shiftKey ? 'B' : 'b';
        checkit = checklen();
        break;
    case 67:
        mychar = e.shiftKey ? 'C' : 'c';
        checkit = checklen();
        break;
    case 68:
        mychar = e.shiftKey ? 'D' : 'd';
        checkit = checklen();
        break;

    default:
        checkit = false;
        break;
    }
    if (!checkit) {
        $('#charLine-1').append('<span class="char">' + mychar + '</span>');
    }
    else {
        $('body').append('<span id="warning">WARNING!</span>');
    }
}

要让它在整个文档上工作:

$(document).ready(function(){
  $(document).keydown(function(e) { 
     checkKey(e);
  });
});

然后只需单击页面并键入字符 - 仅注意上面代码中的“b”、“c”、“d”。

IF you like checking each one use a switch: call the checkKey function as needed passing the event.

function checklen() {
    var typed = $("span.char").length;
    var typedAdjusted = typed + 1;
    var paperWidth = 900;
    var charWidth = 44;
    return (typedAdjusted >= paperWidth % charWidth);
}

function checkKey(e) {
    var mychar = '';
    var checkit = false;
    switch (e.keyCode) {
    case 66:
        mychar = e.shiftKey ? 'B' : 'b';
        checkit = checklen();
        break;
    case 67:
        mychar = e.shiftKey ? 'C' : 'c';
        checkit = checklen();
        break;
    case 68:
        mychar = e.shiftKey ? 'D' : 'd';
        checkit = checklen();
        break;

    default:
        checkit = false;
        break;
    }
    if (!checkit) {
        $('#charLine-1').append('<span class="char">' + mychar + '</span>');
    }
    else {
        $('body').append('<span id="warning">WARNING!</span>');
    }
}

to get it to work on the entire document:

$(document).ready(function(){
  $(document).keydown(function(e) { 
     checkKey(e);
  });
});

then just click on the page and type characters - note only 'b','c','d' on the code above.

美人骨 2025-01-11 15:10:46

如果您观察按键事件,则可以使用 String.fromCharCode(event.keyCode) 来获取输入的字符,而不必弄乱查找表。

function (event) {
    var key = event.keyCode;
    if (key > 31 && key < 127) return String.fromCharCode(key);
}

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.

function (event) {
    var key = event.keyCode;
    if (key > 31 && key < 127) return String.fromCharCode(key);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文