gpt4 book ai didi

javascript - 如何防止按 ESC 键(Javascript)中止?

转载 作者:行者123 更新时间:2023-11-30 06:50:03 27 4
gpt4 key购买 nike

我想提示一个字符串。我的额外需求是不要按 ENTER 或 ESC 键退出。使用此方法 ENTER 有效(不会转义):

var f1 = '';
while (f1.length < 1) {
f1 = prompt('Please give a value of the string!');
}

我怎样才能知道这个脚本也不会通过按 ESC 键退出?谢谢,危险品

最佳答案

当您单击 ESC 按钮时,

prompt 返回 null。您还需要在 while 中检查 null

while (f1 == null || f1.length < 1)

或者干脆

while (!f1)

使用这个:

var f1 = '';
while (!f1) {
f1 = prompt('Please give a value of the string!');
}

Note: As Cancel and ESC both returns null, you will not be able to Cancel it though.

关于javascript - 如何防止按 ESC 键(Javascript)中止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58521477/

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