Chr*_*isP 5 keyboard jquery events
我有以下 jQuery 函数
jQuery.fn.integerMask =
function () {
return this.each(function () {
$(this).keydown(function (e) {
var key = (e.keyCode ? e.keyCode : e.which);
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
return (
key == 8 ||
key == 9 ||
key == 46 ||
key == 37 ||
key == 39 ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
);
});
});
};
Run Code Online (Sandbox Code Playgroud)
用于输入数字。问题是 SHIFT+8 会导致输入星号 * 字符。似乎允许将“8”键与 SHIFT 组合使用。如何防止 SHIFT+8 被接受并插入“*”字符?
Too*_*ode -1
<!DOCTYPE html>
<html>
<head>
<script>
function isKeyPressed(event)
{
if (event.shiftKey==1)
{
alert("The shift key was pressed!");
}
else
{
alert("The shift key was NOT pressed!");
}
}
</script>
</head>
<body onmousedown="isKeyPressed(event)">
<p>Click somewhere in the document. An alert box will tell you if you pressed the shift key or not.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
关键字-> event.shiftKey
来源:http ://www.w3schools.com/jsref/tryit.asp?filename=try_dom_event_shiftkey