javascript實(shí)現(xiàn)簡易聊天室
更新時間:2019年07月12日 11:54:44 作者:smile@qingqing
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)簡易聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下
聊天室是我們經(jīng)常見的,比如微信聊天界面、QQ聊天界面等等,一個簡易的聊天室如下:
1.html代碼
<div class="content"> <div class="section"></div> <form action="#"> <textarea id="$value"></textarea> <button type="button" id="sub">發(fā)送</button> </form> </div>
2.css代碼
.content{
border-radius: 5px;
border: 2px solid #cccccc;
width: 500px;
height: 700px;
margin: 50px auto 0;
overflow: hidden;
}
.section{
width: 500px;
height: 569px;
overflow-x: hidden;
overflow-y: auto;
border-bottom: 1px solid #cccccc;
padding-top: 30px;
}
.section::-webkit-scrollbar{
display: none;
}
form{
width: 500px;
height: 100px;
}
form textarea{
outline: none;
border: none;
display: block;
float: left;
width: 370px;
height: 100px;
font-size: 25px;
text-align: top;
line-height: 35px;
resize: none;
}
form button{
outline: none;
border: none;
display: block;
float: left;
width: 130px;
height: 100px;
}
form button:active{
background: #ccc;
}
.line{
width: 500px;
overflow: hidden;
}
.left,.right{
height: auto;
font-size: 25px;
line-height: 50px;
border-radius: 10px;
padding: 0 10px;
overflow-wrap: break-word;
margin-bottom: 20px;
}
.left{
max-width: 400px;
margin-left: 20px;
float: left;
background: rgb(243, 244, 245);
}
.right{
max-width: 400px;
float: right;
margin-right: 20px;
background: rgb(79, 230, 49);
}
3.js代碼
<script type="text/javascript">
function $(str){
if (str[0]=='.') {
return document.getElementsByClassName(str.substring(1));
}else if (str[0]=='#') {
return document.getElementById(str.substring(1));
}else{
return document.getElementsByTagName(str);
}
}
//以上代碼可以單獨(dú)封裝成一個函數(shù)
var count = 0;
$('#sub').onclick=function(){
//當(dāng)點(diǎn)擊發(fā)送按鈕后,創(chuàng)建class名為line的盒子,來盛放聊天的內(nèi)容
var div = document.createElement('div');
div.className = 'line';
$('.section')[0].appendChild(div);
var str = $('#$value').value;
var p = document.createElement('p');
if (count%2==1) {
p.className = 'left';
}else{
p.className = 'right';
}
p.innerHTML = str;
$('#$value').value = '';
div.appendChild(p);
count++;
}
</script>
4.效果圖

好啦,一個簡易的聊天室就制作完啦!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- nodejs實(shí)現(xiàn)的一個簡單聊天室功能分享
- 使用Angular和Nodejs、socket.io搭建聊天室及多人聊天室
- js編寫簡單的聊天室功能
- AngularJS+Node.js實(shí)現(xiàn)在線聊天室
- Ajax PHP JavaScript MySQL實(shí)現(xiàn)簡易無刷新在線聊天室
- JavaScript仿聊天室聊天記錄
- Nodejs實(shí)現(xiàn)多房間簡易聊天室功能
- NodeJS實(shí)現(xiàn)一個聊天室功能
- Node.js中使用socket創(chuàng)建私聊和公聊聊天室
- JavaScript實(shí)現(xiàn)QQ聊天室功能
相關(guān)文章
Map與WeakMap類型在JavaScript中的使用詳解
這篇文章主要介紹了Map與WeakMap類型在JavaScript中的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
原生js canvas實(shí)現(xiàn)鼠標(biāo)跟隨效果
這篇文章主要為大家詳細(xì)介紹了原生js canvas實(shí)現(xiàn)鼠標(biāo)跟隨效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
webpack4 SplitChunks實(shí)現(xiàn)代碼分隔詳解
這篇文章主要介紹了webpack4 SplitChunks實(shí)現(xiàn)代碼分隔詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
javascript Array.prototype.slice的使用示例
javascript Array.prototype.slice除了常見的從某個數(shù)組中抽取出新的數(shù)組外,它還有一些其他的用法,下面就為大家講這些妙用2013-11-11
Typescript中bind的使用方法及注意事項(xiàng)
在TypeScript(以及JavaScript)中,bind()是一個用于改變函數(shù)上下文的方法,下面這篇文章主要給大家介紹了關(guān)于Typescript中bind的使用方法及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2024-08-08

