最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實(shí)現(xiàn)簡易聊天對話框

 更新時間:2017年08月17日 11:47:30   作者:涼介v7  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡易聊天對話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)簡易聊天對話框的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>聊天對話框</title>
<style type="text/css">
*{font-size: 14px; padding:0; margin:0;}
.main{
 position: relative;
 margin: 20px auto;
 border: 1px solid steelblue;
 width: 430px;
 height: 400px;
}
.msgInput{
 display: block;
 width: 406px;
 height: 60px;
 margin: 10px auto;

}
.sendbtn{
 position: absolute;
 width: 100px;
 height: 29px;
 bottom: 5px;
 right: 10px;
}
.content{
 list-style: none;
 width: 410px;
 height: 280px;
 margin: 5px auto;
 border: 1px dotted #D1D3D6;
 overflow-y: scroll;
}
.msgContent{
 width:auto;
 max-width: 250px;
 height: auto;
 word-break: break-all;
 margin: 5px;
 padding: 3px;
 border-radius: 5px;
}

.content .left{
 float: left;
 text-align: left;
 background-color: lightgrey;
}
.content .right{
 float: right;
 text-align: right;
 background-color: yellowgreen;
}


</style>
<script type="text/javascript">
 window.onload=function(){

 var input = document.getElementById('msg_input');//查找緩存
 document.getElementById('sendbtn').onclick=function () {
  //var input1 = document.getElementById('msg_input');//
  //input0

  sendMsg();
 }



 //快捷鍵 發(fā)送
 document.onkeypress = function (event) {
  var e = event || window.event;
  var keycode = e.keyCode || e.which;
  console.log(e)
  if( keycode==10){//判斷同時按下ctrl 和enter
  sendMsg()
  }
 }

 function sendMsg() {
  var input = document.getElementById('msg_input');//查找緩存
  var ul = document.getElementById('content');

  var newLi = document.createElement('li');
  newLi.innerHTML = input.value;
  newLi.className = 'msgContent right';
  ul.appendChild(newLi);

  var div = document.createElement('div');
  div.style = 'clear:both';
  ul.appendChild(div);




  ajax({
  url:'http://jisuznwd.market.alicloudapi.com/iqa/query?question='+input.value,
  success:function (res) {
//  console.log(res)
   var obj = JSON.parse(res);
   console.log(obj)
   var array = obj.result.content;
//   var zhengzhou = array[0];
   var tmp = array;
//   var tmp = '溫度:'+zhengzhou.day_air_temperature+','+zhengzhou.day_weather;
   console.log(tmp)

   var newLi = document.createElement('li');
   newLi.innerHTML = tmp;
   newLi.className = 'msgContent left';
   ul.appendChild(newLi);

   var div = document.createElement('div');
   div.style = 'clear:both';
   ul.appendChild(div);
   input.value = '';
   newLi.scrollIntoView();//將元素滾動到可見位置
  }
  })

  input.value = '';
  newLi.scrollIntoView();//將元素滾動到可見位置
 }

 }



 function ajax(obj) {
 //?lastCursor=6610&pageSize=10
//  var url = 'reg.php';
 var xhr = null;
 if(window.ActiveXObject){
  xhr = new ActiveXObject('Microsoft.XMLHTTP')
 }else{
  xhr = new XMLHttpRequest();
 }
// $username = $_REQUEST['username'];
// $password = $_REQUEST['password'];

 //打開與服務(wù)器的連接
 if(obj.method){
  xhr.open(obj.method,obj.url,true);
 }else{
  xhr.open('get',obj.url,true);
 }
 xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 xhr.setRequestHeader("Authorization","APPCODE 3e9dfb924f464e9593a95f9d2bbf4348")


 // {username:'zhangsa',password:123123}
// sendData = encodeURIComponent(sendData);


 // 發(fā)送請求
 //console.log(res);
 //回調(diào)函數(shù)
 xhr.onreadystatechange = function () {
//  console.log(xhr.readyState)
  if(xhr.readyState == 4){
  //數(shù)據(jù)接收完畢
  if(xhr.status == 200){
//   console.log('請求成功',xhr.responseText)
   if(obj.success){
   obj.success(xhr.responseText)
   }

  }else{
//   console.log(xhr.status,'請求出錯')
   if(obj.failure){
   obj.failure('請求失敗')
   }
  }
  }
 }
//  var sendData = 'username=zhangsan&password=123456';
 if( obj.method == undefined ||obj.method.toLowerCase() =='get'){
  xhr.send(null);//
 }else{
  xhr.send(obj.params);//

 }
 }


</script>

</head>

<body>
 <div id="main" class="main">
 <ul id="content" class="content">
  <li class="msgContent left">hello?</li>
  <div style="clear:both"></div>
  <li class="msgContent left">hello</li>
  <div style="clear:both"></div>
  <li class="msgContent right">hi</li>
  <div style="clear:both"></div>
  <li class="msgContent left">hehe</li>
  <div style="clear:both"></div>
  <li class="msgContent left">goodbye</li>
  <div style="clear:both"></div>
  <li class="msgContent right">。。。。</li>
  <div style="clear:both"></div>
  <li class="msgContent right">sdfasdsadfd fasd fasd fasdfasdfasdf</li>
  <div style="clear:both"></div>
  <li class="msgContent right"> 哈哈</li>
  <div style="clear:both"></div>
 </ul>
 <textarea id="msg_input" class="msgInput"></textarea>
 <button id="sendbtn" class="sendbtn">發(fā)送</button>
 </div>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用TypeScript類型注解的方法詳解

    使用TypeScript類型注解的方法詳解

    這篇文章主要為大家詳細(xì)介紹了TypeScript的類型注解,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • JS學(xué)習(xí)筆記之貪吃蛇小游戲demo實(shí)例詳解

    JS學(xué)習(xí)筆記之貪吃蛇小游戲demo實(shí)例詳解

    這篇文章主要介紹了JS學(xué)習(xí)筆記之貪吃蛇小游戲demo,結(jié)合實(shí)例形式詳細(xì)分析了javascript實(shí)現(xiàn)貪吃蛇小游戲的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05
  • JavaScript中日期函數(shù)的相關(guān)操作知識

    JavaScript中日期函數(shù)的相關(guān)操作知識

    日期函數(shù)是我們經(jīng)常用到的知識點(diǎn),下面通過本文給大家介紹JavaScript中日期函數(shù)的相關(guān)操作知識,非常不錯,感興趣的朋友一起學(xué)習(xí)吧
    2016-08-08
  • 淺談Javascript鼠標(biāo)和滾輪事件

    淺談Javascript鼠標(biāo)和滾輪事件

    淺談Javascript鼠標(biāo)和滾輪事件,鼠標(biāo)事件也許是web頁面當(dāng)中最常用到的事件,因?yàn)槭髽?biāo)是最常用的導(dǎo)航設(shè)備
    2012-06-06
  • js使用highlight.js高亮你的代碼

    js使用highlight.js高亮你的代碼

    本篇文章主要介紹了js使用highlight.js高亮你的代碼 ,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-08-08
  • 100個不能錯過的實(shí)用JS自定義函數(shù)

    100個不能錯過的實(shí)用JS自定義函數(shù)

    本文收集了100個原生態(tài)JavaScript編寫的常用、實(shí)用自定義函數(shù),需要的朋友可以參考下
    2014-03-03
  • 支付寶小程序?qū)崿F(xiàn)省市區(qū)三級聯(lián)動

    支付寶小程序?qū)崿F(xiàn)省市區(qū)三級聯(lián)動

    這篇文章主要為大家詳細(xì)介紹了支付寶小程序?qū)崿F(xiàn)省市區(qū)三級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 十分鐘教你上手ES2020新特性

    十分鐘教你上手ES2020新特性

    這篇文章主要介紹了十分鐘教你上手ES2020新特性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 淺談JavaScript的計時器對象

    淺談JavaScript的計時器對象

    下面小編就為大家?guī)硪黄獪\談JavaScript的計時器對象。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦
    2016-12-12
  • JS判斷對象是否存在的10種方法總結(jié)

    JS判斷對象是否存在的10種方法總結(jié)

    本篇文章主要是對JS判斷對象是否存在的10種方法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-12-12

最新評論

上虞市| 伊吾县| 北流市| 苗栗市| 正阳县| 开江县| 嘉黎县| 泾源县| 梁河县| 晋宁县| 简阳市| 皋兰县| 高尔夫| 靖边县| 金堂县| 鄢陵县| 龙岩市| 兰溪市| 嘉善县| 曲沃县| 云南省| 平原县| 张家口市| 北辰区| 从化市| 台南县| 凤阳县| 东乌珠穆沁旗| 丹江口市| 梅河口市| 盈江县| 房产| 莱芜市| 大方县| 体育| 康马县| 锡林郭勒盟| 军事| 京山县| 旬邑县| 姜堰市|