基于JavaScript实现评论框展开和隐藏功能

所属分类: 网络编程 / JavaScript 阅读数: 1624
收藏 0 赞 0 分享

1.效果图如下所示,

点击评论会在对应的评论区域展开评论输入框,点击取消会取消对应的评论输入框

2.html代码:需要引入jQuery.js

<div
class="nr-comment">
<div
class="nr-comment-con">
<div
class="nr-comment-nav">
<div
class="comment-number">
<span>493</span>
<span>条评论</span>
</div>
<div
class="comment-sort">
切换为时间排序
</div>
</div>
<div
class="comment-content">
<div
class="com-users">
<div
class="comment-user">
<span>知乎用户</span>
<div
class="comment-user-content">
这个爬虫真的好强大!
</div>
</div>
<div
class="comment-time">
<div>2017.10.01 21:32:30</div>
<button
class="btn btn-primary btn-sm btn-reply">回复</button>
</div>
</div>
<div
class="user-reply">
<duv
class="reply-in">
<input
type="text"
value=""
name=""
placeholder="请输入评论内容" />
</duv>
<div
class="reply-buttons">
<button
type="button"
class="btn btn-primary btn-comment btn-sm">评论</button>
<button
type="button"
class="btn btn-default btn-cancel btn-sm">取消</button>
</div>
</div>
</div>
<div
class="comment-content">
<div
class="com-users">
<div
class="comment-user">
<span>知乎用户</span>
<div
class="comment-user-content">
这个爬虫真的好强大!
</div>
</div>
<div
class="comment-time">
<div>2017.10.01 21:32:30</div>
<button
class="btn btn-primary btn-sm btn-reply">回复</button>
</div>
</div>
<div
class="user-reply">
<duv
class="reply-in">
<input
type="text"
value=""
name=""
placeholder="请输入评论内容" />
</duv>
<div
class="reply-buttons">
<button
type="button"
class="btn btn-primary btn-comment btn-sm">评论</button>
<button
type="button"
class="btn btn-default btn-cancel btn-sm">取消</button>
</div>
</div>
</div>
<div
class="comment-content">
<div
class="com-users">
<div
class="comment-user">
<span>知乎用户</span>
<div
class="comment-user-content">
这个爬虫真的好强大!
</div>
</div>
<div
class="comment-time">
<div>2017.10.01 21:32:30</div>
<button
class="btn btn-primary btn-sm btn-reply">回复</button>
</div>
</div>
<div
class="user-reply">
<duv
class="reply-in">
<input
type="text"
value=""
name=""
placeholder="请输入评论内容" />
</duv>
<div
class="reply-buttons">
<button
type="button"
class="btn btn-primary btn-comment btn-sm">评论</button>
<button
type="button"
class="btn btn-default btn-cancel btn-sm">取消</button>
</div>
</div>
</div>
<div
class="comment-content">
<div
class="com-users">
<div
class="comment-user">
<span>知乎用户</span>
<div
class="comment-user-content">
这个爬虫真的好强大!
</div>
</div>
<div
class="comment-time">
<div>2017.10.01 21:32:30</div>
<button
class="btn btn-primary btn-sm btn-reply">回复</button>
</div>
</div>
<div
class="user-reply">
<duv
class="reply-in">
<input
type="text"
value=""
name=""
placeholder="请输入评论内容" />
</duv>
<div
class="reply-buttons">
<button
type="button"
class="btn btn-primary btn-sm btn-comment">评论</button>
<button
type="button"
class="btn btn-default btn-sm btn-cancel">取消</button>
</div>
</div>
</div>
<div
class="comment-ipt">
<input
type="text"
placeholder="输入你的评论">
<button
type="submit"
class="btn btn-sm btn-primary">评论</button>
</div>
</div>
</div>

3.css样式代码,样式无所谓,自己写就可以。

.nr-comment {
width:
100%;
border-right:
1px 
solid #A9A9A9;
border-left:
1px 
solid #A9A9A9;
}
.nr-comment .nr-comment-con {
width:
100%;
}
.nr-comment .nr-comment-con .nr-comment-nav {
width:
100%;
height:
40px;
border-bottom:
1px 
solid #F5F5F6;
border-right:
1px 
solid #A9A9A9;
border-left:
1px 
solid #A9A9A9;
background-color:
#1E8CE0;
}
.nr-comment .nr-comment-con .nr-comment-nav .comment-number {
float:
left;
width:
85px;
height:
30px;
text-align:
center;
margin-top:
5px;
color:
white;
line-height:
2.3em;
}
.nr-comment .nr-comment-con .nr-comment-nav .comment-sort {
float:
right;
width:
110px;
height:
30px;
margin-top:
5px;
margin-right:
10px;
line-height:
2em;
color:
white;
}
.nr-comment .nr-comment-con .comment-content {
width:
100%;
margin-top:
10px;
border-bottom:
1px 
solid #a9a9a9;
}
.nr-comment .nr-comment-con .comment-content .com-users {
width:
100%;
min-height:
60px;
}
.nr-comment .nr-comment-con .comment-content .com-users .comment-user {
float:
left;
width:
500px;
height:
60px;
}
.nr-comment .nr-comment-con .comment-content .com-users .comment-user span {
color:
black;
margin-left:
10px;
}
.nr-comment .nr-comment-con .comment-content .com-users .comment-user .comment-user-content {
width:
90%;
height:
60px;
margin-left:
10px;
}
.nr-comment .nr-comment-con .comment-content .com-users .comment-time {
float:
right;
width:
190px;
height:
60px;
text-align:
center;
color:
#9CADC6;
font-size:
0.9em;
text-align:
right;
}
.nr-comment .nr-comment-con .comment-content .com-users .comment-time div {
margin-right:
15px;
}
.nr-comment .nr-comment-con .comment-content .com-users .comment-time .btn-reply {
margin-top:
5px;
border-radius:
4px;
border:
none;
background-color:
#1BB394;
color:
white;
margin-right:
15px;
}
.nr-comment .nr-comment-con .comment-content .user-reply {
display:
none;
width:
100%;
height:
50px;
}
.nr-comment .nr-comment-con .comment-content .user-reply .reply-in {
float:
left;
width:
85%;
height:
50px;
}
.nr-comment .nr-comment-con .comment-content .user-reply .reply-in input {
width:
100%;
height:
30px;
margin-top:
10px;
margin-left:
10px;
}
.nr-comment .nr-comment-con .comment-content .user-reply .reply-buttons {
float:
right;
margin-top:
10px;
}
.nr-comment .nr-comment-con .comment-content .user-reply .reply-buttons .btn-comment {
float:
right;
margin-right:
14px;
background-color:
#1BB394;
border:
none;
color:
white;
}
.nr-comment .nr-comment-con .comment-content .user-reply .reply-buttons .btn-cancel {
float:
right;
margin-right:
10px;
}
.nr-comment .nr-comment-con .comment-ipt {
width:
100%;
height:
40px;
border-bottom:
1px 
solid #A9A9A9;
margin-top:
10px;
}
.nr-comment .nr-comment-con .comment-ipt input {
display:
block;
width:
92%;
height:
30px;
float:
left;
font-size:
14px;
margin-left:
10px;
}
.nr-comment .nr-comment-con .comment-ipt button {
display:
block;
float:
right;
background-color:
#1BB394;
color:
white;
margin-right:
13px;
border:
none;
}

4.js控制对应评论按钮事件。

<script>
$(document).ready(function()
 {
$('.btn-reply').click(function()
 {
// console.log($(this).index());
// 获取回复按钮集合,getElementByClassName;
var m
= document.getElementsByClassName("btn-reply");
var n
= document.getElementsByClassName("user-reply");
console.log('回复按钮集合'
+ m);
// 获取回复按钮的索引
var index
= $(".btn-reply").index($(this));
console.log(index);
$(".user-reply").eq(index).css("display",
"block");
});
$('.btn-cancel').click(function()
 {
var m
= document.getElementsByClassName("btn-reply");
var n
= document.getElementsByClassName("user-reply");
var index
= $(".btn-cancel").index($(this));
console.log(index);
$(".user-reply").eq(index).css("display",
"none");
});
});
</script>

以上所述是小编给大家介绍的基于JavaScript实现评论框展开和隐藏功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

jQuery 行级解析读取XML文件(附源码)

项目中应用jQuery解析读取XML语言配置文件来实现语言的调度。这是jQuery解析读取XML文件功能的测试源码,现拿出来分享。
收藏 0 赞 0 分享

JS 文件本身编码转换 图文教程

JS编码转换,这句话本身就是一句具有二重义的话。通常理解为JS文件里能转换编码的代码,但是,我所碰到的问题并不是这样的,是要解决JS文件本身的编码问题,它是UTF-8编码的还是ANSI编码的?
收藏 0 赞 0 分享

jQuery Ajax之$.get()方法和$.post()方法

load()方法通常用来从Web服务器上获取静态的数据文件,然而这并不能体现Ajax的全部价值。在项目中,如果需要传递一些参数给服务器中的页面,那么可以使用$.get()或者$.post()方法(或者是后面要讲解到的$.ajax方法)。
收藏 0 赞 0 分享

jQuery Ajax之load()方法

jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是laod()、$.get()和$.post()方法,第3层是$.getScript()和$.getJSON()方法。
收藏 0 赞 0 分享

JavaScript 核心参考教程 内置对象

JavaScript 是根据 "ECMAScript"标准制定的网页脚本语言。这个标准由 ECMA 组织发展和维护。ECMA-262 是正式的 JavaScript 标准。
收藏 0 赞 0 分享

JavaScript 核心参考教程 RegExp对象

JavaScript 核心参考教程RegExp对象,学习正则表达式的朋友可以参考下。
收藏 0 赞 0 分享

javascript hashtable实现代码

javascript中没有像c#,java那样的哈希表(hashtable), 然而,javascript中的Array也只有一些类似于'哈希表'的非常简单功能。
收藏 0 赞 0 分享

百度留言本js 大家可以参考下

百度留言本js 大家可以参考下。
收藏 0 赞 0 分享

javascript 判断某年某月有多少天的实现代码 推荐

以前写网页的时候,经常碰到选择日期的问题,其实就是判断某年某月有多少天。
收藏 0 赞 0 分享

让iframe子窗体取父窗体地址栏参数(querystring)

突然用到,记录一下,对地址栏字符串用正则处理最好,有时间研究一下。 主要是思路。
收藏 0 赞 0 分享
查看更多