JS DOM -- 关于回车键盘事件执行事件
一、需求制作一个模拟对话框,
二、
1、需要发送后,输入框清空
2、按enter键可发送
三、代码部分
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1 { width:240px; height:200px; border:1px solid #333; background:#f1f1f1; padding:10px; }
</style>
<script>
window.onload = function (){
var oDiv = document.getElementById('div1');
var oStrong = document.getElementById('strong1');
var oText = document.getElementById('text1');
var oBtn = document.getElementById('btn1');
var str='';
function Say(){
str += oStrong.innerHTML + oText.value +'<br>';
oDiv.innerHTML = str;
oText.value = '';
}
oBtn.onclick = Say;
document.onkeydown=function(event){
var e = event || window.event;
if(e && e.keyCode==13){ // enter 键
Say();
}
};
};
</script>
</head>
<body>
<div id="div1"></div>
<strong id="strong1">张三:</strong>
<input id="text1" type="text" />
<input id="btn1" type="button" value="提交" />
</body>
</html>
心怀梦想,勿忘初心