サマリー:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">&
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>在線客服系統(tǒng)</title>
<style>
div:nth-child(1){
width: 460px;
height: 660px;
background-color: lightblue;
margin: 30px auto;
color: #333;
box-shadow: 2px 2px 2px #757575;
}
h2{
text-align: center;
margin-bottom: -10px;
}
div:nth-child(2){
width: 400px;
height: 500px;
border: 4px double green;
background-color: #ccc;
margin: 10px auto 10px;
}
ul{
list-style-type: none;
line-height: 2em;
overflow: hidden;
padding: 15px;
}
textarea{
border: none;
resize: none;
background-color: bisque;
width: 340px;
margin-left: 20px;
font-size: 1.5rem;
border-radius: 8px;
}
button{
width: 60px;
height: 40px;
background-color: green;
color: white;
border: none;
border-radius: 8px;
}
button:hover {
cursor: pointer;
background-color: coral;
}
</style>
</head>
<body>
<div>
<h2>在線客服</h2>
<div>
<ul>
<li></li>
</ul>
</div>
<table>
<tr>
<td align="right"><textarea name="text" id="" cols="50" rows="4"></textarea></td>
<td align="left"><button type="button">發(fā)送</button></td>
</tr>
</table>
</div>
<script>
//獲取頁(yè)面中的相關(guān)元素
var btn = document.getElementsByTagName('button')[0];
var text = document.getElementsByName('text')[0];
var list = document.getElementsByTagName('ul')[0];
var sum = 0;
//添加點(diǎn)擊事件,獲取用戶說(shuō)的內(nèi)容并且發(fā)送到窗口
btn.onclick = function(){
//獲取用戶提交的內(nèi)容
if(text.value.length===0){
alert('客官,你想說(shuō)些什么呢?')
return false;
}
var userComment = text.value; //將用戶提交的內(nèi)家獲取并保存
text.value = ''; //同時(shí)將用戶留言區(qū)清空
// 創(chuàng)建一個(gè)li
var li = document.createElement('li');
//用戶頭像
var userPic = '<img src="images/c16.jpg" width="30" style="border-radius:50%">';
li.innerHTML = userPic +' ' + userComment;
list.appendChild(li); //將用戶的信息添加到聊天窗口中來(lái)
sum += 1;
// 設(shè)置回復(fù)定時(shí)器,2秒鐘后自動(dòng)回復(fù)
setTimeout(function(){
//自動(dòng)回復(fù)的信息模板
let info = [
'今天的天氣真是不錯(cuò)',
'感謝您光顧本店,這里肯定有你心愛(ài)的物品',
'聽(tīng)說(shuō)ET最近要來(lái)地球,親,有空多看看天空',
'時(shí)間不早了,親,注意身體,早點(diǎn)休息',
'你好',
'我很好',
'吃飯了沒(méi)有?',
'祝你天天開(kāi)心。',
'賀喜發(fā)財(cái)。',
];
let temp = info[Math.floor(Math.random()*8)];
// 創(chuàng)建一個(gè)li
let reply = document.createElement('li');
//客服頭像
let kefuPic = '<img src="images/u55.jpg" width="30" style="border-radius:50%">';
reply.innerHTML = kefuPic + ' ' +'<span style="color:red">' +temp+'</span>';
list.appendChild(reply); //將用戶的信息添加到聊天窗口中來(lái)
sum += 1;
},2000);
//大于10行時(shí),清空窗口,并將計(jì)數(shù)器清零
if (sum>10){
list.innerHTML = ' ';
sum = 0;
}
}
</script>
</body>
</html>
添削の先生:天蓬老師添削時(shí)間:2019-04-24 15:35:12
先生のまとめ:這個(gè)案例, 就是考察最基本的dom操作....
dom操作是js最常用的場(chǎng)合