触发 onkeypress
属性事件当用户按下一个键时。
与onkeydown事件相关的事件顺序:
没有。
<element onkeypress="script">
所有HTML元素,EXCEPT:
<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, <title>
onkeypress |
Yes | Yes | Yes | Yes | Yes |
<!DOCTYPE html>
<html>
<body>
<input type="text" onkeypress="myFunction()">
<script>
function myFunction() {
console.log("pressed a key");
}
</script>
</body>
</html>
下面的代码显示了如何听TextArea的按键事件。
<html>
<head>
<script type="text/javascript">
function handleEvent(oEvent) {
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n>" + oEvent.type;
}
</script>
</head>
<body>
<P>Type some characters into the first textbox.</p>
<P>
<textarea id="txtInput" rows="15" cols="50"
onkeydown="handleEvent(event)" onkeyup="handleEvent(event)"
onkeypress="handleEvent(event)"></textarea>
</p>
<P>
<textarea id="txt1" rows="15" cols="50"></textarea>
</p>
</body>
</html>