前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jquery ajax聊天室

jquery ajax聊天室

作者头像
用户5760343
发布2019-10-10 14:33:09
1.4K0
发布2019-10-10 14:33:09
举报
文章被收录于专栏:sktjsktj

image.png

html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> body{ margin:0; padding:0; font-size:12px; } #messagewindow { height: 250px; border: 1px solid; padding: 5px; overflow: auto; } #wrapper { margin: auto; width: 438px; } </style> <script src="../scripts/jquery.js" type="text/javascript"></script> <script type="text/javascript"> //<![CDATA[

(function(){ //定义时间戳 timestamp = 0; //调用更新信息函数 updateMsg(); //表单提交
(function(){ //定义时间戳 timestamp = 0; //调用更新信息函数 updateMsg(); //表单提交

("#chatform").submit(function(){

.post("backend.php",{ message:
.post("backend.php",{ message:

("#msg").val(), name:

("#author").val(), action: "postmsg", time: timestamp }, function(xml) { //清空信息文本框内容
("#author").val(), action: "postmsg", time: timestamp }, function(xml) { //清空信息文本框内容

("#msg").val(""); //调用解析xml的函数 addMessages(xml); }); return false; //阻止表单提交 }); }); //更新信息函数,每隔一定时间去服务端读取数据 function updateMsg(){

.post("backend.php",{ time: timestamp }, function(xml) { //移除掉 等待提示
.post("backend.php",{ time: timestamp }, function(xml) { //移除掉 等待提示

("#loading").remove(); //调用解析xml的函数 addMessages(xml); }); //每隔4秒,读取一次. setTimeout('updateMsg()', 4000); } //解析xml文档函数,把数据显示到页面上 function addMessages(xml) { //如果状态为2,则终止 if(

("status",xml).text() == "2") return; //更新时间戳 timestamp =
("status",xml).text() == "2") return; //更新时间戳 timestamp =

("time",xml).text(); //

.each循环数据
.each循环数据

("message",xml).each(function() { var author =

("author",this).text(); //发布者 var content =
("author",this).text(); //发布者 var content =

("text",this).text(); //内容 var htmlcode = "<strong>"+author+"</strong>: "+content+"<br />"; $("#messagewindow").prepend( htmlcode ); //添加到文档中 }); } //]]> </script>

</head> <body>

代码语言:javascript
复制
<div id="wrapper">
    <p id="messagewindow"><span id="loading">加载中...</span></p>
    <form id="chatform" action="#">
        姓名: <input type="text" id="author" size="50"/><br />
        内容: <input type="text" id="msg"  size="50"/>   <br /> 
        <input type="submit" value="发送" /><br />
    </form>
</div>

</body> </html>

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.10.09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • html
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档