codecamp

HTML事件属性onkeydown

HTML事件属性onkeydown


触发 onkeydown 属性事件当用户按下一个键。

与onkeydown事件相关的事件顺序:

  • onkeydown
  • onkeypress
  • onkeyup


HTML5中的新功能

没有。

句法

<element onkeydown="script">

支持的标签

所有HTML元素,EXCEPT:

<base>, 
<bdo>, 
<br>, 
<head>, 
<html>, 
<iframe>, 
<meta>, 
<param>, 
<script>, 
<style>, 
<title>


浏览器兼容性

onkeydown Yes Yes Yes Yes Yes

例子

<!DOCTYPE html>
<html>
<body>

<input type="text" onkeydown="myFunction()">

<script>
function myFunction() {
    console.log("pressed a key");
}
</script>

</body>
</html>

Click to view the demo

实施例2

下面的代码在body标签中为key down事件添加事件处理程序输出从键代码转换的键字符。

<html>
<head>
<script type="text/javascript">
  function press() {
    var oTextbox = document.getElementById("txt1");
    oTextbox.value += "\n>" + String.fromCharCode(event.keyCode);
  }
</script>
</head>
<body onkeydown="press()">
  <P>
    <textarea id="txt1" rows="15" cols="50"></textarea>
  </p>
</body>
</html>

Click to view the demo

HTML事件属性ondrag
HTML事件属性ononline
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }