开发者

Javascript: addEventListener with onkeydown doesn't seem to work

开发者 https://www.devze.com 2022-12-22 04:53 出处:网络
If you replace \"onkeydown\" with \"click\", it reacts, at least. <input id=\"yourinput\" type=\"text\" />

If you replace "onkeydown" with "click", it reacts, at least.

<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 d开发者_StackOverflowidn't.");
    }
}
</script>


The event type should be "keydown", notice that you don't need the on prefix:

element.addEventListener("keydown", keyDownTextField, false);

Note also that you should get the keyCode from the event object in your handler:

function keyDownTextField (e) {
  var keyCode = e.keyCode;
  //...
}

Check an example here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号