jQuery實(shí)現(xiàn)簡單聊天室
自從看了jQuery后,頓時感覺其的確很簡單易學(xué)。下面就一把自己寫的一個簡易的聊天室程序?qū)懗鰜怼?/p>
主要就是利用jQuery的ajax,然后,別的其實(shí)也沒什么了。先上client端的程序吧。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" type="text/css" href="./styles/chat-mobile.css" rel="external nofollow" />
<script type="text/javascript" src="./scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function() {
timestamp = 0;
updateMsg(timestamp);
$('button').click(function() { //重點(diǎn)是這里,從這里向服務(wù)器端發(fā)送數(shù)據(jù)
$.post('chat.php', {
'message': $('#msg').val(),
'name': $('#name').val(),
'timestamp': timestamp
},function(xml) {
$('#msg').val('');
addMessage(xml);
});
return false;
});
$('#name').blur(function (){ //僅僅用來控制名字輸入框的背景
if ($('#name').val()) {
$(this).css({"background": "url(images/background.jpg)", "border": "2px dashed #fff"});
};
});
$('#name').click(function () {
$(this).css({"background": "#fff", "border": "2px solid #fff"});
})
});
//update message
function updateMsg(timestamp) { //從服務(wù)器端更新聊天數(shù)據(jù),并載入吧
$.post('chat.php', {'timestamp': timestamp}, function(xml) {
$('#loading').remove();
addMessage(xml);
});
setTimeout('updateMsg(timestamp);', 1000); //1s刷新一次信息
}
function addMessage(xml) { //解析xml,并添加到頁面內(nèi)
if($('status', xml).text() == 2) {
return;
};
timestamp = $('timestamp', xml).text();
$('message', xml).each(function() {
var author = $('author', this).text();
var content = $('content', this).text();
var time = $('time', this).text();
var htmlcode = '<div><strong>' + author + ': </strong><label>' + time + '</label><p>' + content + '</p></div>';
$('#messageWindow').append(htmlcode);
scrollToBottom();
});
}
function scrollToBottom () { //控制滾動條一直顯示在底部
var height = document.getElementById('messageWindow').scrollHeight;
if (height > $('#messageWindow').scrollTop()) {
$('#messageWindow').scrollTop(height);
}
}
</script>
</head>
<body>
<header>
<div id="hr"></div>
</header>
<div id="wrapper">
<div id="window">
<div id="messageWindow">
<span id="loading">loading...</span>
</div>
<form id="chatform">
<label>your message:</label>
<textarea type="text" id="msg" size="50"/></textarea>
<input type="text" id="name" size="10" placeholder="your name"/>
<button accesskey="s">Send</button>
</form>
</div>
</div>
<br/>
<br/>
<p id="hint">Hint: 移動版的,開放的聊天室</p>
<footer>
<p>©SamuraiMe</p>
</footer>
</body>
</html>
服務(wù)器端我是用php寫的簡單程序,把產(chǎn)生的xml貼出來。其實(shí)就是簡簡單單給數(shù)據(jù)庫存數(shù)據(jù)取數(shù)據(jù)的一個過程。
<?xml version="1.0" encoding="utf-8"?> <response> <status></status> <timestamp></timestamp> <message> <author></author> <content></content> <time></time> </message> </response>

上面就是最后成品,手機(jī)截圖不方便,就在firefox上截了,效果是一樣的。雖然css寫得磕磣了點(diǎn)。但是還是有需要注意的地方。就像圖上'how are you ...'那一大串,如果換成'1111...111'一大串,就會產(chǎn)生scroll-x,移動端好像沒有出現(xiàn)滾動條,消息就看不到了。這怎么能容忍。于是要加
word-wrap: break-word;
本來我以為要寫字符串程序,自己手動來換行,有了word-wrap就一句話搞定了。
最后要說,jQuery的確是簡單好用,但終究是一個輕量級庫,很多事情是完成不了的,就比如讓那個滾動條一直顯示在底部,我就發(fā)現(xiàn)無法用jQuery完成,也許是我才看jQuery,還沒找到獲取scrollHeight的方法,最后只能回到不熟悉的js上面去。
var height = document.getElementById('messageWindow').scrollHeight;
if (height > $('#messageWindow').scrollTop()) {
$('#messageWindow').scrollTop(height);
}
差點(diǎn)忘了說了。
<meta name="viewport" content="width=device-width"/>
在移動端有一個虛擬可視區(qū)域,比實(shí)際的可視區(qū)域要大一些。第一次在手機(jī)上查看的時候,總有scroll出現(xiàn),用了這個meta就可以解決了。
最后,這是我第一篇技術(shù)博客。希望對看到的人能有所幫助。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于jQuery實(shí)現(xiàn)簡單人工智能聊天室
- nodejs實(shí)現(xiàn)的一個簡單聊天室功能分享
- 使用Angular和Nodejs、socket.io搭建聊天室及多人聊天室
- AngularJS+Node.js實(shí)現(xiàn)在線聊天室
- js編寫簡單的聊天室功能
- Ajax PHP JavaScript MySQL實(shí)現(xiàn)簡易無刷新在線聊天室
- JavaScript仿聊天室聊天記錄
- Nodejs實(shí)現(xiàn)多房間簡易聊天室功能
- Node.js中使用socket創(chuàng)建私聊和公聊聊天室
- 基于javascript、ajax、memcache和PHP實(shí)現(xiàn)的簡易在線聊天室
相關(guān)文章
基于jQuery替換table中的內(nèi)容并顯示進(jìn)度條的代碼
這個例子使我更加明白呈現(xiàn)數(shù)據(jù)是前端工作滴一部分,如何使table中的數(shù)值變?yōu)榍逦貤l狀圖呢?聽我細(xì)細(xì)道來2011-08-08
JavaScript jQuery 中定義數(shù)組與操作及jquery數(shù)組操作
這篇文章主要介紹了JavaScript jQuery 中定義數(shù)組與操作及jquery數(shù)組操作的相關(guān)資料,需要的朋友可以參考下2015-12-12
基于jQuery的圖片大小自動適應(yīng)實(shí)現(xiàn)代碼
這個和以前弄的圖片遠(yuǎn)處放大有許多相同的地方,比如圖片預(yù)加載、有限容器顯示無限大圖片。2010-11-11
jQuery使用$.extend(true,object1, object2);實(shí)現(xiàn)深拷貝對象的方法分析
這篇文章主要介紹了jQuery使用$.extend(true,object1, object2);實(shí)現(xiàn)深拷貝對象的方法,結(jié)合實(shí)例形式分析了jQuery中$.extend(true,object1, object2);進(jìn)行深拷貝操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03
Validform+layer實(shí)現(xiàn)漂亮的表單驗(yàn)證特效
創(chuàng)建一個JavaScript表單驗(yàn)證插件,可以說是一個繁瑣的過程,涉及到初期設(shè)計、開發(fā)與測試等等環(huán)節(jié)。實(shí)際上一個優(yōu)秀的程序員不僅是技術(shù)高手,也應(yīng)該是善假于外物的。本文就給大家結(jié)合2款優(yōu)秀的插件來實(shí)現(xiàn)這個問題,有需要的小伙伴可以參考下2016-01-01

