gpt4 book ai didi

Javascript(学习)

转载 作者:行者123 更新时间:2023-11-29 20:04:13 29 4
gpt4 key购买 nike

我正在从“JavaScript 圣经”一书中学习 javascript,但我遇到了一些困难。我试图理解这段代码:

function checkIt(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
status = "This field accept only numbers."
return false
}
status = ""
return true
}

谁能给我解释一下?

最佳答案

我假设您只是想让我们解释代码的作用。如果是这样,请参见下文:

// Create a function named checkIt, which takes an argument evt.
function checkIt(evt) {

// If the user has passed something in the argument evt, take it.
// Else, take the window.event as evt.
evt = (evt) ? evt : window.event;

// Get the Character Code. It can be either from evt.which or evt.keyCode,
// depending on the browser.
var charCode = (evt.which) ? evt.which : evt.keyCode;

// If the Character is not a number, the do not allow the user to type in.
// The reason for giving 31 to 48 and greater than 57 is because, each key
// you type has its own unique character code. From 31 to 48, are the numeric
// keys in the keyboard.
if (charCode > 31 && (charCode < 48 || charCode > 57)) {

// Give a status informing the user.
status = "This field accept only numbers.";

// Prevent the default action of entering the character.
return false;
}

// Clear the status
status = "";

// Allow the user to enter text.
return true;
}

ASCII码引用


(来源:cdrummond.qc.ca)


(来源:cdrummond.qc.ca)

PS:我编辑了您的代码,添加了分号 ;,但分号丢失了。

关于Javascript(学习),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12889442/

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