- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
例如,如果我有这个:
<input type='text' onkeydown='doSomething()' />
<script>
function doSomething() {
// ?
}
</script>
最佳答案
你已经接近了,但你的代码的问题是你正在执行 doSomething()
回应keyDown
事件...让我们仔细看看——你正在执行 doSomething
没有传递任何参数。即使您确实传递了参数,您也没有命名/接受参数。
对您的代码进行一些快速调整,一切就绪:
// Notice an `event` argument, containing the important data
function doSomething(event) {
console.log('****** doSomething');
console.log('event.keyCode: ', event.keyCode);
console.log('event.which: ', event.which);
console.log('event.key: ', event.key);
console.log('event.code: ', event.code);
}
<!-- Notice: We pass the key `event` object into doSomething -->
<input type='text' onkeydown='doSomething(event)' placeholder="Type here..." />
event.key
(
TL;DR:尝试使用这个 )
KeyboardEvent.key
Documentation ,
event.key
如果您正在寻找不被弃用的“面向 future ”的方式,这是推荐的方式。文档正在大量修改中,因此 MDN 上的细节很少。
event.key
,在 IE11 以下或 Safari 上根本不支持(截至 2017 年 1 月)。这可能已经是一个交易破坏者,所以让我们继续寻找。
event.keyCode
event.keyCode
, 其中
MDN says is now deprecated并附带此警告:
You should avoid using this if possible; it's been deprecated for some time. Instead, you should use
KeyboardEvent.code
, if it's implemented. Unfortunately, some browsers still don't have it, so you'll have to be careful to make sure you use one which is supported on all target browsers. Google Chrome and Safari have implementedKeyboardEvent.keyIdentifier
, which was defined in a draft specification but not the final spec.
keydown
之间有细微的差异。和
keypress
事件(
not the same thing! )使其不再值得使用。 RIP
keyCode
,这是真的!
event.which
The numeric code for a particular key pressed, depending on whether an alphanumeric or non-alphanumeric key was pressed. Please see KeyboardEvent.charCode** and KeyboardEvent.keyCode for more details.
keyCode
我们已经知道它也被弃用了,那么
charCode
是什么?关于?
event.charCode
keypress
的 Unicode 字符代码事件...和
we get another warning from MDN :
Do not use this property anymore, it is deprecated. Use
KeyboardEvent.key
instead.
KeyboardEvent.key
是面向 future 的方法,但尚未得到充分支持...它还返回
0
一直为
keydown
事件,你必须听
keypress
事件才有值(value)。
event.code
event.code
docs from MDN :
represents a physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value which isn't altered by keyboard layout or the state of the modifier keys.
ShiftRight
这样的东西。与
ShiftLeft
相比,对于游戏开发和非常具体的交互很有用。所有代码选项的文档底部都有一个很棒的图表。
event.keyCode
和
event.which
属性作为 where
event.key
的后备和
event.code
不能使用。
keycode.info
关于javascript - 跨浏览器跨操作系统获取 keyCode/字符按下 onkeydown 事件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15108670/
德尔福2010 我在主窗体上使用 OnFormKeyDown 事件,并且基本上在对话框上使用相同的事件 //主窗体 procedure TfrmMain.FormKeyDown(Sender: TOb
我在网上看到的大部分代码都使用了document.onkeydown,但是MDN只列出了window.onkeydown . This comment还建议使用 window.onkeydown。使用
我在线性布局中放置了一个 ListView ,我想覆盖 ListView 中的 onkeydown() 方法,我不控制焦点,只是改变onkeydown()中的一些变量,我希望系统像我没有覆盖onkey
我想知道如何使用事件监听器在 javscript 中正确使用 onkeydown 和 onkeyup 事件。我希望在您只需在网站中而不是在文本字段中按下时监听该事件。 我不确切地知道脚本会是什么样子,
我在从数字键盘输入数字值时遇到问题。我的脚本仅接受“qwerty”键上方数字键的数字。我想要的是用户还可以从数字键盘输入数字。以下是 HTML: 但是我尝试在 onkeypress 和
我不知道这个问题是否已经在某个地方得到了回答,但就是这样。 为什么会这样? el.onkeydown = function(e) { if(!((e.keyCode > 95 && e.keyCo
我不知道这个问题是否已经在某个地方得到了回答,但就是这样。 为什么会这样? el.onkeydown = function(e) { if(!((e.keyCode > 95 && e.keyCo
我在 if 语句中使用 var "str"时遇到问题。我知道这是一个范围问题,但我不知道如何解决它。我已经失败了好几次了。 我的目标是在 if 语句中使用“str”的值来显示或不显示警报。限制是我只能
当用户在 Android 中按下后退按钮时,我会出现一个自定义对话框,询问“您确定吗”。问题是,如果我实现 onKeyDown 方法,音量硬按钮就不起作用,并且音量不会升高或降低(在整个 Activi
我正在编写代码,以在按下音量调高/调低键时更改 TextView,以更新系统音量%设置的 TextView。我现在有这段代码,该代码可以工作一次,但它会覆盖调高/调低的功能,是有没有办法重写它或排除它
使用Material-UI Table我有这个: console.log(event)}> ... 当我用鼠标单击表格,然后开始单击向上和向下箭头键时,控制台上没有任何记录。我
设置左右箭头键图像导航脚本时遇到问题 document.onkeydown = function(event) { var e = event || wind
在我的旧项目中,我在 winform TextBox 中使用了 OnKeyPress 的覆盖来替换一些输入键 if(e.KeyChar == 'a') e.KeyChar = 'b'; // j
function Test(event) { console.log(event.key + ' ' + event.which + ' ' + event.code); } 结果: undef
这是我的代码 document.onkeydown = function (a) { if (a.which == 13) { alert("Not Anymore");
我正在处理一个表单并尝试创建一个执行以下操作的 onkeydown 函数: 仅在按下 tab 键 时触发(正常工作) 插入一个新元素(input)(一切正常) 然后插入的 input 调用相同的 on
我有一个无法识别第一次按键的 onKeyDown 事件(甚至无法进入该事件,我已经通过生成“toast”输出进行了测试)。在第二次按键和之后,它完美地工作。如果我点击屏幕上的另一个元素并再次尝试按键,
在我的应用程序中,我尝试覆盖 onKeyDown 方法并调用自定义对话框,但是当在设备上运行时,它没有显示该自定义对话框。我使用了调试器并看到调用了 onKeyDown 方法,但没有出现对话框并且应用
这个问题在这里已经有了答案: Override home and back button is case a boolean is true (6 个答案) 关闭 8 年前。 我覆盖了 Activi
public boolean onKeyDown(int keyCode, KeyEvent event) { } 此方法中的代码仅在选定的 EditText 具有退格键条目时触发。我需要它被任何类型
我是一名优秀的程序员,十分优秀!