亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

在線客服案例作業(yè)

原創(chuàng) 2019-01-01 00:48:16 195
摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>DOM實戰(zhàn):模擬智能在線客服系統(tǒng)</title>   <style type="text/css"> &n
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM實戰(zhàn):模擬智能在線客服系統(tǒng)</title>
  <style type="text/css">
    div:nth-child(1){
      width: 450px;
      height: 650px;
      background-color: lightskyblue;
      color: #333;
      box-shadow: 2px 2px 2px $808080;
    }

    h2 {
      text-align: center;
      margin-bottom: -10px;
    }

    div:nth-child(2){
      width: 400px;
      height: 500px;
      border: 2px solid #ccc;
      background: #efefef;
      margin: 20px auto 10px;
    }

    ul {
      list-style-type:none;
      line-height: 2em;
      overflow:hidden;
      padding: 15px;
    }

    table{
      width: 280px;
      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><textarea name="text" cols="50" rows="4"></textarea></td>
      <td><button type="button">發(fā)送</button></td>
    </tr>
  </table>
</div>
</body>
<script type="text/javascript">
  //獲取到頁面中的相關(guān)元素
  let btn = document.getElementsByTagName('button')[0];
  let text = document.getElementsByName('text')[0];
  let list = document.getElementsByTagName('ul')[0];
  var sum = 0; //計數(shù)器

  //添加點擊事件,獲取用戶說的內(nèi)容兵發(fā)送到窗口
  btn.onclick = function(){
    //獲取用戶提交的內(nèi)容
    if(text.value.length === 0){
      alert('你沒有輸入信息,請輸入');
      return false;
    }

    let userComment = text.value; //將用戶提交的內(nèi)容獲取并保存
    text.value = ''; //立即將用戶留言區(qū)清空

    // 創(chuàng)建一個li
    let li = document.createElement('li');
    li.innerHTML = userComment; //文本框的內(nèi)容
    // 用戶頭像
    let userPic = '<img src="inc/touxiang1.jpg" width="32"; style="border-radius:50%">';
    li.innerHTML = userPic + '' + userComment;
    list.appendChild(li); //將用戶信息添加到聊天窗口中
    sum += 1;

    //設(shè)置定時器,2秒自動回復(fù)
    setTimeout(function(){
      //自動回復(fù)信息的模版
      let info = [
        '您好,有什么可以幫助你?',
        '除了退貨,退款,咱們什么都可以聊',
        '說啥,本姑娘怎么聽不懂呢?再說一遍',
        '在我方便的時候再回復(fù)你吧~~',
        '沒有事情,就關(guān)閉會話吧',
      ];

      let temp = info[Math.floor(Math.random()*4)];
      let reply = document.createElement('li');
      let kefuPic = '<img src="inc/touxiang2.jpg" width="32"; style="border-radius:50%">';
      reply.innerHTML =kefuPic + '' +'<span style="color:red;float:right;">'+temp+'</span>';
      list.appendChild(reply);
      sum += 1;
    },2000);

    //清空窗口并將計數(shù)器清零
    if(sum > 10){
      list.innerHTML = '';
      sum = 0;
    }
  }
</script>
</html>

QQ截圖20190101003737.jpg

總結(jié):

1、用value.length === 0 前面點上變量,用if來判斷是否有輸入內(nèi)容;

2、使用text.value;將數(shù)據(jù)保存,然后用text.value = '';  清空留言區(qū)

3、let userPic = '<img src="inc/touxiang1.jpg" width="32"; style="border-radius:50%">';  用html直接引入頭像,直接定義變量,然后使用appendChild 添加信息

4、定時器setTimeout(function(){

},1500); 的用法

5、info.[Math.floor(Math.random()*4)]; 隨機獲取info變量里的數(shù)組中其中1條。


批改老師:天蓬老師批改時間:2019-01-01 09:13:28
老師總結(jié):document.getElementsByTagName('button')[0];推薦改成: document.getElementsByTagName('button').item(0), 這樣是不是更好看些呢?

發(fā)布手記

熱門詞條