问题描述
我正在尝试检测Javascapslock,但是下面的代码始终返回false.Firefox和IE Web控制台表示未定义kc和sk.事件"e"
似乎包含一个which元素,但是 e.which
是未定义的.
I am trying to detect capslock in Javascript, however the code below always returns false. Firefox and IE web console says that kc and sk are undefined. Event "e"
seems to contain a which element, however e.which
is undefined.
我做错了什么吗?我正在使用devexpress(这可能是个问题吗?)
Am I doing something incorrect? I am using devexpress (could this be an issue?)
JavaScript
<script>
function isCapslock(e) {
kc = e.keyCode ? e.keyCode : e.which;
sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk))
return true;
else
return false;
}
function showCapsWarning(e){
function showCapsWarning(e) {
if (isCapslock(e)) {
document.getElementById("capsWarningDiv").style.visibility = 'visible';
}
else {
document.getElementById("capsWarningDiv").style.visibility = 'hidden';
}
}
</script>
aspx文件
<dx:ASPxTextBox ID="tbPassword" runat="server" ClientInstanceName="tbPassword" Password="True" ToolTip="Please enter your password."
Width="300px"
ClientSideEvents-KeyPress="function(s,e) {showCapsWarning(e); }"
>
这是我从以下位置获得capslock javascript代码的地方: http://www.codeproject.com/Articles/17180/Detect-Caps-Lock-with-Javascript
This is where I got the capslock javascript code from: http://www.codeproject.com/Articles/17180/Detect-Caps-Lock-with-Javascript
推荐答案
e..我注意到事件对象正在使用以下路径以便到达which元素: event.htmlEvent.which
.一旦我使用了 e.htmlEvent
,它便开始正常工作.
e.which was undefined within the web developer console. I noticed that the event object was using the following path in order to get to the which element: event.htmlEvent.which
. Once I used e.htmlEvent
it began to work correctly.
这篇关于Capslock检测在Javascript中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!