abstrak:<!DOCTYPE html><html><head><meta charset="UTF-8"><title>模擬智能在線客服系統(tǒng)1</title><style type="text/css">div:nth-child(1) {width: 450px;height: 6
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>模擬智能在線客服系統(tǒng)1</title>
<style type="text/css">
div:nth-child(1) {width: 450px;height: 650px;background-color: lightskyblue;margin: 30px auto;color: #333;box-shadow: 5px 5px 10px #808080}
h2 {text-align: center; margin-bottom: -10px;}
div:nth-child(2) {width: 400px;height: 500px;border: 4px double green;border-radius: 20px;background-color: #efefef;margin: 20px auto 10px;}
ul {list-style: none;line-height: 2em;overflow: hidden;padding: 15px;}
table {width: 400px;height:80px;margin: auto;}
textarea{border: none;resize: none; background-color: lightyellow;}
button {width: 60px;height: 40px;background-color: seagreen;color: white;border: none;}
button:hover {cursor: pointer;background-color: orange;}
</style>
</head>
<body>
<div>
<h2>在線客服</h2>
<div contenteditable="true">
</div>
<table>
<tr>
<td><textarea cols="44" rows="3" name="text"></textarea></td>
<td><button type=button>發(fā)送</button></td>
</tr>
</table>
</div>
<script type="text/javascript">
//獲取到頁面中的按鈕,文本域,對話內(nèi)容區(qū)
let btn = document.getElementsByTagName('button')[0];
let text = document.getElementsByName('text')[0];
let list = document.getElementsByTagName('div')[1];
let sum = 0;
//添加按鈕點(diǎn)擊事件,獲取用戶數(shù)據(jù)并推送到對話窗口中
btn.onclick = function () {
let usert
if (text.value.length == 0) {
usert='<span style="color:blue;">請輸入內(nèi)容!!!</span>'
}else{
usert = text.value;
}
//立即清空用戶信息區(qū)
text.value = '';
//創(chuàng)建一個新節(jié)點(diǎn)li
let co = document.createElement('p');
co.innerHTML = usert; //將用戶輸入的內(nèi)容添加到新節(jié)點(diǎn)中
// let userleft = '<img src="inc/peter.jpg" width="30" style="border-radius:50%">'; // 設(shè)置用戶頭像
co.style.textAlign='left'
// console.log(co)
// li.innerHTML = userPic + ' ' + userComment; // 將用戶頭像與用戶的聊天內(nèi)容合并
list.appendChild(co); //發(fā)送聊天內(nèi)容,實(shí)際上就是將新節(jié)點(diǎn)插入到對話列表中
sum += 1; // 聊天記錄自增1
//設(shè)置定時器,默認(rèn)2秒后會自動回復(fù)
setTimeout(function(){
let info = [
'1的回答',
'2的回答',
'3的回答',
'4的回答',
'5的回答'
];
// let temp = info[Math.floor(Math.random()*4)];
let temp
// console.log(typeof(usert))
if (usert.indexOf("1")>=0){temp=info[0]}
else if (usert.indexOf('2')>=0){temp=info[1]}
else if (usert.indexOf('3')>=0){temp=info[2]}
else if (usert.indexOf('4')>=0){temp=info[3]}
else if (usert.indexOf('5')>=0){temp=info[4]}else{temp='我不知道!!!'}
let reply = document.createElement('p');
reply.innerHTML=temp
reply.style.textAlign='right'
reply.style.color='red'
console.log(reply)
list.appendChild(reply);
sum += 1;
},2000);
// 當(dāng)聊天記錄達(dá)到10條時清空窗口
if (sum > 10) {
list.innerHTML = '';
sum = 0;
}
}
</script>
</body>
</html>
Guru membetulkan:韋小寶Masa pembetulan:2019-03-05 09:18:45
Rumusan guru:兩個作業(yè)差的還是不太多的 但是還是有點(diǎn)想法的 很不錯 要大膽的去創(chuàng)新和改變