请解释这个Javascript代码 [英] please explain this Javascript code

查看:67
本文介绍了请解释这个Javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function isNumberKey(evt)
     {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
           return false;

        return true;
     }





此代码允许用户只输入txtbx中的数字........

bt我不知道它是如何工作的......

请在深度解释我这段代码....



this code allows user to input only number in txtbx........
bt I dont know how it works...
please explain me this code in deep....

推荐答案

此代码检查按键事件,鼠标按下



evt.which: -

event.which属性规范化event.keyCode和event.charCode。建议观看event.which用于键盘输入键。

event.which还可以按下按钮(mousedown和mouseupevents),报告左按钮1,中间2,右3。使用event.which而不是event.button。

有些浏览器使用keyCode,其他浏览器使用keyCode。



evt.keycode: -

在按键事件中,按下的键的Unicode值存储在keyCode或charCode属性中,而不是两者都存储。如果按下的键生成一个字符(例如'a'),则charCode被设置为该字符的代码,尊重字母大小写。 (即charCode考虑是否按住shift键)。否则,按下的密钥的代码存储在keyCode中。





你的这一行检查键盘上按下了哪个键,它得到了ascii没有。那个键

var charCode =(evt.which)? evt.which:event.keyCode



验证带有ascii no的charcode,范围从0到9
This code checks the event of key press, mouse down

evt.which :-
The event.which property normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input.
event.which also normalizes button presses (mousedown and mouseupevents), reporting 1 for left button, 2 for middle, and 3 for right. Use event.which instead of event.button.
Some browsers use keyCode, others use which.

evt.keycode:-
In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.


Your this line checks which key pressed on keyboard and it gets ascii no. of that key
var charCode = (evt.which) ? evt.which : event.keyCode

after it validates that charcode with ascii no which is ranging from 0 to 9


?意味着三元运算符作为if else like条件运行

这个函数可以放在文本框上,如onKeypress =return isnumberKey();

当你按下一个按钮然后一个按键事件会调用



假如你有除数字值以外的其他按钮那么除了数值之外它不会在文本框中打印任何内容



var charCode =(evt.which)? evt.which:event.keyCode //假设您按了A键,那么此Ascii值将存储在charcode中,现在它将使用if检查条件(charCode> 31 &&(charCode< 48 || charCode> 57))因为这个A不属于这个范围所以它将返回false并且它不会在文本框中打印任何值



尝试和letme知道
? means ternary operator which run as a if else like condition
this function you can place on textbox like onKeypress="return isnumberKey();"
when you will press a button then a keypress event will call

Suppose if you have press other than numeric value then it will not print anything in your textbox other than numeric value

var charCode = (evt.which) ? evt.which : event.keyCode// Suppose you pressed "A" key then this Ascii value will store in charcode now it will check condition with if (charCode > 31 && (charCode < 48 || charCode > 57)) as this "A" doesnot fall in this range thus it will return false and it will not print any value in textbox

try and letme know


这篇关于请解释这个Javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆