jquery学习练习:制作QQ简易聊天框

2019-03-2115:53:10WEB前端开发Comments13,866 views字数 1374阅读模式

使用html( )获取和设置页面内容
使用val( )获取聊天内容
使用addClass( )为指定元素追加样式
使用数组保存聊天人员头像和昵称
使用随机函数获取聊天人员的头像和昵称
在输入框中输入内容,单击“发送”按钮,显示聊天内容,使用addClass( )为聊天内容添加背景颜色、边框为圆角;聊天内容发送完毕后,清空输入框
聊天内容较多,聊天内容在垂直方向显示滚动条
输入框中没有输入内容,单击“发送”按钮,不作任何操作
使用数组保存聊天者的头像和昵称文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10160.html

使用val()获取输入框中的内容,使用length属性判断是否输入内容
获取当前聊天内容
使用html( )把当前聊天内容显示在聊天区域文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10160.html

使用addClass( ) 为当前聊天内容添加背景样式文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10160.html

jquery学习练习:制作QQ简易聊天框文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10160.html

$(document).ready(function(){
var $imgs=new Array("images/head01.jpg","images/head02.jpg","images/head03.jpg");
var $ni=new Array("时尚伊人","六月奇迹","松松");
$("#send").click(function(){
var $sui = parseInt(Math.random() * 3);
var $nei=$("textarea").val();
if($nei.length==""){
alert("你没写东西啊");
}else{
var $currentstr=$(".chatBody").append("<div><img src='"+$imgs[$sui]+"'/>"+$ni[$sui]+"<br/><p >"+$nei+"</p></div>");
$(".chatBody p").addClass("a");
$("textarea").val("");
}
});
});
<body>
<section>
<div></div>
<div><img src="images/icon.jpg"></div>
<textarea></textarea>
<div><span>关闭(C)</span><span>发送(S)</span></div>
</section>
</body>
*{margin: 0; padding: 0; line-height: 22px; font-family: "Arial", "微软雅黑";}
#chat{margin: 3px auto 0 auto; width:436px; border: 1px #999999 solid;}
.chatBody{width: 100%; height: 220px; overflow:auto;}
.chatText{border: none; width: 100%; height: 50px;}
.btn{text-align: right;}
.btn span{display: inline-block; padding: 0 10px; height: 25px;
overflow: hidden; color: #ffffff; border-radius: 5px; background-color: #069dd5; font-size: 12px; margin-right: 3px; cursor:pointer;}
.a{
background-color: darkgray; border-radius: 5px;
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10160.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10160.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10160.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/10160.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定