jQuery实现论坛发帖Demo
效果展示

思路
主要知识点:运用jQuery对HTML元素节点的操作(append)来添加帖子。
交互设计:用户点击页面上的“论坛发帖”按钮,出现表单,填写表单后点击“发帖”按钮,jQuery获取表单内容,创建新的节点(头像随机)来合成一个“帖子”,最终插入到当前页面的结尾。
代码
完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>论坛发帖</title>
<style>
h2 {
text-align: center;
padding: 10px;
background-color: cornflowerblue;
border: 1px solid cornflowerblue;
border-radius: 6px;
color: white;
margin-bottom: 30px;
letter-spacing: 0.8em;
cursor: pointer;
}
.post {
width: 50%;
height: 400px;
margin: 0 auto;
border: 1px solid rgb(221, 221, 221);
border-radius: 2px;
display: none;
position: absolute;
top: 100px;
background-color: white;
left: 25%;
}
.title {
width: 87%;
margin-left: 5%;
margin-top: 20px;
height: 2em;
border: 1px solid rgb(230, 230, 230);
padding-left: 1em;
}
.text {
width: 85%;
margin-left: 5%;
margin-top: 10px;
height: 60%;
border: 1px solid rgb(230, 230, 230);
padding: 1em;
font-size: 1.1em;
overflow: auto;
}
.combtn {
width: 30%;
margin-top: 10px;
margin-left: 35%;
height: 10%;
border: 1px solid rgb(230, 230, 230);
background-color: cornflowerblue;
color: white;
border-radius: 5px;
}
.comment_block {
margin: 0 auto;
width: 50%;
height: auto;
}
.touxiang {
width: 60px;
height: 60px;
float: left;
}
.touxiang img {
border: 1px white solid;
border-radius: 50%;
width: 100%;
height: 100%;
}
.content {
float: left;
width: 80%;
margin-left: 3%;
border: 1px solid rgb(221, 221, 221);
padding: 1em;
border-radius: 5px;
height: auto;
margin-bottom: 20px;
}
h4 {
font-size: 1.2em;
font-weight: normal;
margin-top: 0;
border-left: 6px solid cornflowerblue;
padding-left: 3%;
background-color: rgba(100, 149, 237, 0.2);
border-radius: 5px;
margin-bottom: 3%;
font-family: '宋体';
}
.com_body {
font-family: '宋体';
color: darkgray;
height: auto;
clear: both;
width: 96%;
margin: 0 auto;
margin-bottom: 10px;
/*text-indent: 2em;*/
}
.date {
width: 100%;
text-align: right;
font-size: 0.8em;
color: rgb(114, 114, 114);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
ul {
list-style: none;
}
</style>
</head>
<body>
<div class="comment">
<h2>论坛发帖</h2>
<section>
<ul></ul>
</section>
<div class="post">
<input type="text" class="title" placeholder="请输入标题">
<textarea class="text" placeholder="请输入评论"></textarea>
<input type="button" value="发布评论" class="combtn">
</div>
</div>
<script src="jquery-1.8.3.min.js"></script>
<script>
$(document).ready(function() {
$("h2").click(function() {
$(".post").show()
})
var tou = new Array("1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg");
$(".post .combtn").click(function() {
var $newLi = $("<li></li>"); //创建一个新的li节点元素
var iNum = Math.floor(Math.random() * 10); //随机获取头像
var $block = $("<div class=comment_block></div>");
var $touImg = $("<div class=touxiang><img src=stitch/" + tou[iNum] + "></div>"); //创建头像节点
var $comment = $("<div class=content></div>");
var $title = $("<h4>" + $(".title").val() + "</h4>"); //设置标题节点
var $newComBody = $("<div class=com_body><pre>" + $(".text").val() + "</pre></div>"); //创建一个新的div节点元素
var myDate = new Date();
var currentDate = myDate.getFullYear() + "-" + parseInt(myDate.getMonth() + 1) + "-" + myDate.getDate() + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
var $newDate = $("<div class=date>" + currentDate + "</div>") //在div节点中插入发布时间;
$($block).append($touImg); //插入头像
$($comment).append($title); //插入标题
$($comment).append($newComBody); //插入评论内容
$($comment).append($newDate);
$($block).append($comment);
$($newLi).append($block);
$(".comment section ul").append($newLi);
$(".post .text").val("");
$(".post .title").val("");
$(".post").hide();
})
});
</script>
</body>
</html>
头像素材如下:

改进空间
1.交互方面可以再做得精细些,比如用户不想发帖时可以隐藏发帖表单;
2.布局考虑响应式,字体大小使用相对单位;
3.添加删帖/按时间排序功能;
jQuery实现论坛发帖Demo的更多相关文章
- jquery制作论坛或社交网站的每天打卡签到特效
效果:http://hovertree.com/texiao/jquery/50/ 现在许多社区,购物等网站都设置签到功能,打开可以收获经验.虚拟币等,提高用户粘性,增加浏览量,是一个不错的功能.本文 ...
- 适应手机端的jQuery图片滑块动画DEMO演示
在线预览 下载地址 实例代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- xTiNt 论坛发帖辅助软件 1.0 绿色版
软件名称:xTiNt 论坛发帖辅助软件 1.0 绿色版软件语言: 简体中文授权方式: 免费软件应用平台: Win7 / Vista / Win2003 / WinXP / Win2008 软件大小: ...
- 滚动加载|页面滑到底部加载数据|jquery.endless-scroll插件|使用demo
<html> <head> <link rel="dns-prefetch" href="http://i.tq121.com.cn&quo ...
- ECharts 初识(基于MVC+jQuery+Angularjs实现的Demo)
一.背景: 我们这行做web开发的,很多时候都需要做数据统计报表,现在我所使用的是来自百度团队的ECharts.官方网址:http://echarts.baidu.com/ 我们知 ...
- jquery 图片轮播demo实现
转载注明出处!!! 转载注明出处!!! 转载注明出处!!! 图片轮播demo,弄清楚过程其实是一个很简单的东西,看网上都没有什么实质性的代码,就自己把过程捋了一遍实现了. 这次因为随手写的,所以没有做 ...
- jquery版悬浮模块demo
在做在线客服时,代码就是按照该模块命名.现在,我要添加一个返回主页的功能,我觉得再复制一遍之前的代码,那个量有点多,如果我再添加一个功能,那个量会很多……现在我用创建对象字面量的方式来创建(其实我还想 ...
- Jquery Mobile 百度地图 Demo
首先非常感谢franck分享的Demo! Demo截图: 下面是franck对此Demo的说明: 原理:1.通过百度拾取坐标系统获得点位的坐标. http://api.map.baidu.com/lb ...
- jQuery插件Flot实战Demo
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 复选框、单选框 jquery判断是否选中Demo
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="eachcheckbox.a ...
随机推荐
- Cplex-opl解决网络路由选择和资源分配问题
安装Cplex 注意事项:全英文系统.安装路径.代码,会减少软件运行设置错误.(该软件对中文支持性不好) opl语言基础 [转载]CPLEX学习笔记二 – OPL的数据类型 - 知乎 (zhihu.c ...
- 洛谷P4571 [JSOI2009] 瓶子和燃料
题目 https://www.luogu.com.cn/problem/P4571 思路 首先观察并且简单模拟一下火星人取燃料的过程,发现最终燃料的量一定是他选的k个瓶子容量的线性组合(观察操作3就知 ...
- nginx 解决 405 not allowed错误
1.http nginx.conf文件 error_page 后 增加代码 error_page 405 =200 @405; location @405 { proxy_method GET; pr ...
- /etc/login.defs 创建用户的默认设置文件
/etc/login.defs 文件用于在创建用户时,对用户的一些基本属性做默认设置,例如指定用户 UID 和 GID 的范围,用户的过期时间,密码的最大长度,等等.需要注意的是,该文件的用户默认配置 ...
- Nginx+UWSGI+supervisor
Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检 ...
- TypeScript - 配置文件 tsconfig.json
tsconfig.json 文件 创建两种方式: 1. 直接在根目录新建tsconfig.config.json (配置文件需要自己配置) 2. 执行tsc --init (会自动创建相关配置) t ...
- 计算机网络基础(3):IP与子网掩码/ ping/ ipconfig/ VLAN/ 网络服务器配置
chapter4 构建中型网络 1. IP地址与子网掩码 A类地址:网络ID开头是0,范围从00000001到01111110,126个,其中0 127留作他用.在每个网段里(网络ID),可以容纳2* ...
- bigdecimal 比较大小、bigdecimal 数学运算、bigdecimal 精度
创建 BigDecimal 建议使用 public BigDecimal(String val),使用 number 参数可能会有精度问题 设置精度 setScale(3, BigDecimal.RO ...
- python 实现两个多维数组去重处理
a = [[1,2,3,4],[5,6,7,8],[11,12,13,14],[23,24,12,11]] b = [[5,6,7,8],[23,24,12,11]] c = [] for i in ...
- Linux下查看服务器PCIE Slot槽位信息
在linux系统下,可以使用"dmidecode –t 9"或者"dmidecode –t slot"命令查看PCIE Slot的信息,如下图所示RH2288H ...