[英]Javascript: addEventListener with onkeydown doesn't seem to work
If you replace "onkeydown" with "click", it reacts, at least. 如果用“点击”替换“onkeydown”,它至少会做出反应。
<input id="yourinput" type="text" />
<script type="text/javascript">
document.getElementById("yourinput").addEventListener("onkeydown", keyDownTextField, false);
function keyDownTextField() {
alert("functional");
if(keycode==13) {
alert("You hit the enter key.");
}
else{
alert("Oh no you didn't.");
}
}
</script>
The event type should be "keydown"
, notice that you don't need the on prefix: 事件类型应为"keydown"
,请注意您不需要on前缀:
element.addEventListener("keydown", keyDownTextField, false);
Note also that you should get the keyCode
from the event object in your handler: 另请注意,您应该从处理程序中的事件对象获取keyCode
:
function keyDownTextField (e) {
var keyCode = e.keyCode;
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.