js實(shí)現(xiàn)聊天對(duì)話框
本文實(shí)例為大家分享了js實(shí)現(xiàn)聊天對(duì)話框的具體代碼,供大家參考,具體內(nèi)容如下
<!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("請(qǐng)勿發(fā)送空內(nèi)容");
}
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("請(qǐng)勿發(fā)送空內(nèi)容");
}
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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于p5.js 2D圖像接口的擴(kuò)展(交互實(shí)現(xiàn))
這篇文章主要為大家詳細(xì)介紹了基于p5.js 2D圖像接口的擴(kuò)展,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
JS逆向之?webpack?打包站點(diǎn)實(shí)戰(zhàn)原理分享
本文主要介紹了JS逆向之webpack打包站點(diǎn)實(shí)戰(zhàn)原理分享,webpack是前端程序員用來(lái)進(jìn)行打包JS的技術(shù),打包之后的代碼特征非常明顯,更多相關(guān)知識(shí)需要的小伙伴可以參考下面文章詳細(xì)內(nèi)容2022-06-06
ES6(ECMAScript 6)新特性之模板字符串用法分析
這篇文章主要介紹了ES6(ECMAScript 6)新特性之模板字符串用法,簡(jiǎn)單介紹了ES6模板字符串的概念、功能并結(jié)合實(shí)例形式分析了ES6模板字符串的用法,需要的朋友可以參考下2017-04-04
JavaScript利用正則表達(dá)式替換字符串中的內(nèi)容
本文主要介紹了JavaScript利用正則表達(dá)式替換字符串中內(nèi)容的具體實(shí)現(xiàn)方法,并做了簡(jiǎn)要注釋,便于理解。具有一定的參考價(jià)值,需要的朋友可以看下2016-12-12
在layui中l(wèi)ayer彈出層點(diǎn)擊事件無(wú)效的解決方法
今天小編就為大家分享一篇在layui中l(wèi)ayer彈出層點(diǎn)擊事件無(wú)效的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
一文讓你徹底弄懂js中undefined和null的區(qū)別
JavaScript是一門動(dòng)態(tài)類型語(yǔ)言,元素除了表示存在的空值外,還有可能根本就不存在,這就是undefined存在的原因,這篇文章主要給大家介紹了關(guān)于undefined和null區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-03-03
JavaScript實(shí)現(xiàn)有限狀態(tài)機(jī)的示例代碼
有限狀態(tài)機(jī)(Finite?State?Machine,?FSM)是一種數(shù)學(xué)模型,用于描述系統(tǒng)在不同狀態(tài)下的行為,本文給大家介紹JavaScript實(shí)現(xiàn)有限狀態(tài)機(jī)的示例代碼,感興趣的朋友一起看看吧2024-12-12
webpack配置完熱更新會(huì)刷新整個(gè)頁(yè)面
本文主要介紹了webpack配置完熱更新會(huì)刷新整個(gè)頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

