-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (46 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var sendText = document.getElementById('send-text');
sendText.onkeyup = function (e) {
// console.log(e.keyCode)
if (e.keyCode === 13) {
renderDom('mine', this.value);
ajax({
// url: 'https://developer.duyiedu.com/edu/turing/chat',
url: 'http://localhost:3000/chat',
method: 'get',
data:{
text: this.value
},
isAsync: true,
success: function (res) {
console.log(res)
renderDom('robot', res.text)
}
});
this.value = "";
}
}
// 渲染对话内容
/**
*
* @param {String} who 可选值: mine / robot
* @param {String} text
*/
function renderDom(who, text) {
// 当前对话框的class类名
var divClass = who;
// 当前对话框的头像
var avatorImg = ""
if (who === 'mine') {
divClass += ' clearfix';
avatorImg = "./img/3.png";
} else {
avatorImg = "./img/dog1.jpg";
}
var oDiv = document.createElement('div');
oDiv.className = divClass;
oDiv.innerHTML = ` <img class="avator" src="${avatorImg}" alt="">
<div class="text">${text}</div>`
var content = document.querySelector('.content');
content.appendChild(oDiv);
content.scrollTop = content.scrollHeight;
}