搜索

完成客服聊天截面

原创 2019-03-10 13:31:57 205
摘要:<!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>实战</title>

<style>

div:nth-child(1){

width: 450px;

height: 650px;

background-color: lightblue;

margin: 30px auto;

color:#333;

box-shadow: 2px 2px 2px #808080;

}

h2{

text-align: center;

margin-bottom:-10px;

}

div:nth-child(2){

width: 400px;

height: 500px;

border: 4px double green;

background-color: #efefef;

margin:20px auto 10px;

}

ul{

list-style-type: none;

line-height: 2em;

overflow: hidden;

padding: 15px;

}

table{

width: 90%;

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>

<ul>

<li></li>

</ul>

</div>

<table>

<tr>

<td align="rigth"><textarea name="text" cols="50" rows="4"></textarea></td>

<td align="left"><button type="button">发送</button></td>

</tr>

</table>

</div>

<script>

//获取到页面中的相关元素

let btn =document.getElementsByTagName('button')[0];

let text = document.getElementsByName('text')[0];

let list = document.getElementsByTagName('ul')[0];

let sum = 0;

//添加点击事件,获取用户留言并发送到窗口

btn.onclick = function(){

if(text.value.length === 0){

alert('小子,你啥也没说啊?');

return false;

}

let userComment = text.value; //将用户提交的内容获取并保存

text.value = '';//立即将用户发言区清空


//创建一个li

let li = document.createElement('li');

//用户头像

let userPic = '<img src="/inc/0.jpg" width="30" style="border-radius:50%">';

li.innerHTML = userPic + ' ' +userComment;

list.appendChild(li);

sum +=1;


setTimeout(function(){

let info = [

'真烦人,有话快说!',

'除了退货,什么都可以聊!',

'说的啥,本姑娘咋听不懂?',

'在我觉得方便的时候在回复你!',

'投诉我?投诉我的人多了,你算老几啊?'

];

let temp = info[Math.floor(Math.random()*4)];

let reply = document.createElement('li');

let kefuPic = '<img src="/inc/1.jpg" width="30" style="border-radius:50%">';

reply.innerHTML = kefuPic +' ' + '<span style="color:red">'+temp+'</span>';

list.appendChild(reply);

sum +=1;

},2000);

//清空窗口,条数够10条的时候

if(sum >10){

list.innerHTML = '';

sum = 0;

}

}

</script>

</body>

</html>

1.png

总结:这样的小项目跟着老师做,感觉很简单!用到的知识点也不多!但是自己要做出来,还需要时间练习!

批改老师:韦小宝批改时间:2019-03-10 14:14:07
老师总结:两次的作业是一样的 跟着老师做感觉很简单 自己完全独立去写就慌了 这还是因为写的太少了 动手太少了

发布手记

热门词条