JavaScript实战二——聊天页面
实现功能如下:
完成基本的页面布局;
点击发送实现聊天功能;
正确显示聊天功能;
能够区分A/B的聊天。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat</title>
<style>
*{
background-color:lightcyan;
margin:0px;
}
.box{
width:600px;
height:620px;
background-color: orange;
margin: 8px;
border:solid 2px;
}
.header{
background-color: orange;
border: tomato;
text-align:center;
width:580px;
height: 20px;
margin-left: 10px;
line-height: 20px;
}
.text{
width: 580px;
margin:8px auto 0;
height: 530px;
background-color: white;
border:solid 2px;
overflow: auto;
}
.Amsg{
margin: 0px;
background-color: white;
padding: 4px;
}
.Amsg span{
display: inline-block;
background-color:aqua;
border-radius: 5px;
color:black;
padding:5px;
font-size: medium;
}
.Bmsg{
margin: 0px;
text-align: right;
background-color: white;
}
.Bmsg span{
display: inline-block;
background-color:orange;
border-radius: 5px;
color:black;
padding:5px;
font-size: large;
}
.footer{
width:580px;
margin :10px;
height: 40px;
}
.form {
background-color: orange;
display: flex;
height: 40px;
}
.member {
width: 50px;
height: 35px;
font-size: 10px;
margin-top: 2.5px;
padding:0px;
border:solid 2px;
}
#message {
width: 500px;
height: 32px;
line-height: 30px;
margin-top: 1.5px;
margin-left: 3px;
margin-right: 3px;
padding: 0px;
}
.sm {
width: 50px;
height: 35px;
line-height: 30px;
margin-top: 2.5px;
margin-left:3px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var Chatmsg=document.getElementById("MSG");
var subbutton=document.getElementById("submit");
var Chat=document.getElementById("message");
var Whosay=document.getElementById("who");
subbutton.onclick=function(){
var str="";
if(Chat.value==""){
alert("消息不能为空,请输入消息");
return ;
}
if(Whosay.value==0){
str='<div class="Amsg">'+'A:'+'<span id="Asay">'+Chat.value+'</span></div>';
Chat.value="";
}else{
str='<div class="Bmsg"><span id="Bsay">'+Chat.value+'</span>'+':B'+'</div>';
Chat.value="";
}
Chatmsg.innerHTML=Chatmsg.innerHTML+str;
}
}
</script>
</head>
<body>
<div class="box">
<div class="header">聊天窗口</div>
<div class="text" id="MSG">
<div class="Amsg">
A:<span id="Asay">吃了吗?</span>
</div>
<div class="Bmsg">
<span id="Bsay">还没吃!</span>:B
</div>
</div>
<div class="footer">
<div class="form">
<select class="member" id="who">
<option value="0">A说</option>
<option value="1">B说</option>
</select>
<input type="text" name="聊天信息" id="message">
<button class="sm" id="submit">发送</button>
</div>
</div>
</div>
</body>
</html>