抽奖小程序怎么做_基于jQuery完成简略人工智能谈

2021-01-12 14:19 jianzhan
基于jQuery实现简单人工智能聊天室       这篇文章主要为大家详细介绍了基于jQuery实现简单人工智能聊天室的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

1.当然是聊天啦~点击飞机按钮或者回车可以发送消息到面板。
2.输入特定的内容,系统会给你相应的回复(这里我只设置了Hello,How are you和询问时间的自动回复)。
3.点击叉叉可以清除面板上的所有聊天记录。
4.问时间的时候,根据现在的时间,会给你相应的不同的回复,比如现在是22-23点,系统会回复你“good night”。
5.随着聊天的进行,聊天面板右侧的滚动条会一直维持在最底部。

HTML:

 div 
 /div 
 form 
 input type="text" placeholder="Say Something" 
 button type="button" title="Send Message" 
 i aria-hidden="true" 
 /button 
 button type="reset" title="Reset Message" 
 i aria-hidden="true" 
 /button 
 button type="button" title="Clear the Chat Box" 
 i aria-hidden="true" 
 /button 
 /form 
 footer 
 Designed By 
 a href="alenhhy" rel="external nofollow" target="_blank" 
 Alen Hu 
 /footer 

 *使用了Bootstrap3框架。

JQuery:

$(document).ready(function() { 
 //send the message by click 
 $(".chat-send").click(sendMsg); 
 //press enter to send 
 $("form").keypress(function(event) { 
 if (event.keyCode === 13) { 
 event.preventDefault(); 
 sendMsg(); 
 }); 
 //clear the chat box 
 $(".chat-clear").click(clearChatBox); 
//send message to chat box 
function sendMsg() { 
 var msg = $(".chat-message"); 
 var msgVal = msg.val(); 
 var chatBox = $(".chat-box"); 
 if (msgVal) { 
 var msgAppend = " p span id='you' You: /span " + msgVal + " /p hr 
 chatBox.append(msgAppend); 
 } else {} 
 //dialog reply 
 dialog(msgVal); 
 //empty input 
 msg.val(""); 
 //keep the scroll in bottom 
 chatBox.scrollTop(chatBox[0].scrollHeight); 
//set up the AI dialog 
function dialog(msg){ 
 var replyArr = ["Hi, how's it going :)","I'm good, thx, U :)"]; 
 msg = msg.toLowerCase(); 
 var time = new Date(); 
 var hour = time.getHours(); 
 var minute = time.getMinutes(); 
 var currentTime = plusZero(hour) + ":" + plusZero(minute); 
 var chatBox = $(".chat-box"); 
 if(msg.indexOf("hello") != -1){ 
 chatBox.append(" p span id='ai' AI: /span " + replyArr[0] + " /p hr 
 else if(msg.indexOf("how are you") != -1 || msg.indexOf("how are u") != -1){ 
 chatBox.append(" p span id='ai' AI: /span " + replyArr[1] + " /p hr 
 else if(msg.indexOf("time") != -1){ 
 chatBox.append(" p span id='ai' AI: /span Current Time: " + currentTime + ". " + timeGreeting(hour) + "~ :) /p hr 
 else {} 
//add 0 if time number is 10 
function plusZero(x){ 
 if(x 10){ 
 x = "0" + x; 
 else { 
 x = x; 
 return x; 
//greeting by hour 
function timeGreeting(h){ 
 var greeting = ["U need to sleep","Good morning","Lunch time now","Feel asleep Have some coffee","Free time~Yeah","Good night"]; 
 if(h =0 h =6){ 
 return greeting[0]; 
 else if(h =7 h =10){ 
 return greeting[1]; 
 else if(h =11 h =13){ 
 return greeting[2]; 
 else if(h =14 h =18){ 
 return greeting[3]; 
 else if(h =19 h =21){ 
 return greeting[4]; 
 else if(h =22 h =23){ 
 return greeting[5]; 
 else { 
 return ""; 
//clear the chat box 
function clearChatBox() { 
 $(".(""); 
} 

DEMO在这儿,欢迎FORK:。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持凡科。