00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
document.onkeypress = function(){
//按下字符键触发
console.log("onkeypress")
}
document.onkeydown = function(){
//按下任意键时触发
console.log("onkeydown")
}
/*
【注】(altKey)(ctrlKey)(shiftKey)
altKey altKey属性,bool类型,表示发生事件的时候alt键是否被按下,按下返回true,否则返回false
ctrlKey ctrlKey属性,bool类型,表示发生事件的时候ctrl键是否被按下,按下返回true,否则返回false
shiftKey shiftKey属性,bool类型,表示发生事件的时候shift键是否被按下,按下返回true,否则返回false
*/
document.onkeydown = function(e){
var evt = e || event
console.log(evt.altKey, evt.ctrlKey, evt.shiftKey)
//判断是否按了某个键
/*
KeyCode对照表
通过对照表来查看按了什么按键
*/
console.log(evt.keyCode)
}
</script>
</head>
<body>
</body>
</html>
01
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0px;
}
div{
width: 500px;
height: 500px;
border: 1px solid #cceecc;
}
</style>
</head>
<body>
<div></div>
<input type="text"><input type="button" value="发布">
<script>
//点击按钮使文本框里的内容传到div里
var oDiv = document.getElementsByTagName("div")[0]
var Input = document.getElementsByTagName("input")
Input[1].onclick = commentTxt
function commentTxt(){
oDiv.innerHTML += Input[0].value;
}
Input[0].onkeydown = function(e){
var evt = e || event
if(evt.shiftKey && evt.keyCode == 13){
commentTxt()
}
}
</script>
</body>
</html>