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(){
//按下字符鍵觸發(fā)
console.log("onkeypress")
}
document.onkeydown = function(){
//按下任意鍵時觸發(fā)
console.log("onkeydown")
}
/*
【注】(altKey)(ctrlKey)(shiftKey)
altKey altKey屬性,bool類型匪傍,表示發(fā)生事件的時候alt鍵是否被按下冠息,按下返回true,否則返回false
ctrlKey ctrlKey屬性荠瘪,bool類型夯巷,表示發(fā)生事件的時候ctrl鍵是否被按下,按下返回true哀墓,否則返回false
shiftKey shiftKey屬性趁餐,bool類型,表示發(fā)生事件的時候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="發(fā)布">
<script>
//點(diǎn)擊按鈕使文本框里的內(nèi)容傳到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>