js實現(xiàn)聊天對話框
更新時間:2020年02月08日 11:28:32 作者:since �
這篇文章主要為大家詳細介紹了js實現(xiàn)聊天對話框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)聊天對話框的具體代碼,供大家參考,具體內容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.box{
width:300px;
height:400px;
border:1px solid blue;
margin:0 auto;
}
.inTer{
width:280px;
height:280px;
border:1px solid deeppink;
margin:0 auto;
margin-top:10px;
overflow-y:auto;
}
textarea{
display:block;
width: 276px;
height:65px;
margin:0 auto;
margin-top:5px;
}
#btn{
display:block;
float:right;
margin-right:10px;
margin-top:5px;
}
p{
display:inline-block;
border-radius:5px;
background:#dcdcdc;
font-size:12px;
padding:5px 5px;
margin:5px 0;
margin-left:5px;
max-width:140px;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="box">
<div class="inTer" id="father">
<p>你好</p ></br>
<p>你好</p ></br>
<p>你好</p ></br>
<p>heiheiheiheiehieheieheieheieheiheiehe</p ></br>
</div>
<textarea style="resize: none;" id="txt">
</textarea>
<input type="button" id="btn" value="發(fā)送" />
</div>
</body>
</html>
<script>
var btn = document.getElementById("btn");
var txt = document.getElementById("txt");
var father = document.getElementById("father")
var p = document.getElementsByTagName("p");
btn.onclick=function(){
if(txt.value==""){
alert("請勿發(fā)送空內容");
}
else{
var son = document.createElement("p");
son.style.backgroundColor="yellowgreen";
son.style.clear="both";
son.style.float="right";
son.style.marginRight="5px";
son.innerText=txt.value;
father.appendChild(son);
txt.value="";
son.scrollIntoView();
}
}
document.onkeydown=function(evt){
var e = evt || event;
e.keyCode=e.which=e.charCode;
if(e.keyCode==13 || e.keyCode==10){
if(txt.value==""){
alert("請勿發(fā)送空內容");
}
else{
var son = document.createElement("p");
son.style.backgroundColor="yellowgreen";
son.style.clear="both";
son.style.float="right";
son.style.marginRight="5px";
son.innerText=txt.value;
father.appendChild(son);
txt.value="";
son.scrollIntoView();
}
}
}
</script>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JS逆向之?webpack?打包站點實戰(zhàn)原理分享
本文主要介紹了JS逆向之webpack打包站點實戰(zhàn)原理分享,webpack是前端程序員用來進行打包JS的技術,打包之后的代碼特征非常明顯,更多相關知識需要的小伙伴可以參考下面文章詳細內容2022-06-06
ES6(ECMAScript 6)新特性之模板字符串用法分析
這篇文章主要介紹了ES6(ECMAScript 6)新特性之模板字符串用法,簡單介紹了ES6模板字符串的概念、功能并結合實例形式分析了ES6模板字符串的用法,需要的朋友可以參考下2017-04-04
在layui中l(wèi)ayer彈出層點擊事件無效的解決方法
今天小編就為大家分享一篇在layui中l(wèi)ayer彈出層點擊事件無效的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
一文讓你徹底弄懂js中undefined和null的區(qū)別
JavaScript是一門動態(tài)類型語言,元素除了表示存在的空值外,還有可能根本就不存在,這就是undefined存在的原因,這篇文章主要給大家介紹了關于undefined和null區(qū)別的相關資料,需要的朋友可以參考下2022-03-03
JavaScript實現(xiàn)有限狀態(tài)機的示例代碼
有限狀態(tài)機(Finite?State?Machine,?FSM)是一種數(shù)學模型,用于描述系統(tǒng)在不同狀態(tài)下的行為,本文給大家介紹JavaScript實現(xiàn)有限狀態(tài)機的示例代碼,感興趣的朋友一起看看吧2024-12-12

