jQuery show/addClass 对于许多具有相同类的 div
所以我想做的是:
HTML 代码:
<div class="div_with_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue
lectus sed tortor placerat quis porttitor dui vehicula. Morbi molestie
tortor sit amet eros tempor consectetur.
</div>
<div class="show_more_btn">Show more</div>
<div class="div_with_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue
lectus sed tortor placerat quis porttitor dui vehicula. Morbi molestie
tortor sit amet eros tempor consectetur.
</div>
<div class="show_more_btn">Show more</div>
<div class="div_with_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue
lectus sed tortor placerat quis porttitor dui vehicula. Morbi molestie
tortor sit amet eros tempor consectetur.
</div>
<div class="show_more_btn">Show more</div>
CSS 代码:
.showMore
{
display:block;
}
.div_with_content
{
display:none;
}
所以我有很多带有“div_with_content”类的 div(不显示),其中包含一些内容,通常是文本。每个“div_with_content”下方都有一个包含在 div“show_more_btn”中的按钮。因此,当我按下 div show_more_btn 时,将会添加 showMore 类的样式(通过 jQuery 中的 addClass)来缓慢显示该 div。所以它很容易实现:
JS 代码:
$(function() {
$(".show_more_btn").click(function(event){
$(".div_with_content").addClass("showMore").show(1500,"swing");
});
});
但只有一个问题:当我按下任一 show_more_btn 时,所有 div_with_content 都会慢慢显示,而不仅仅是 show_more_btn 上的一个。所以我想要的是:当我按“show_more_btn”时,只会显示“show_more_btn”上的“div_with_content”,而其他“div_with_content”仍然不可见。我该如何实现这一目标?
So what I'm tring to do:
HTML code:
<div class="div_with_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue
lectus sed tortor placerat quis porttitor dui vehicula. Morbi molestie
tortor sit amet eros tempor consectetur.
</div>
<div class="show_more_btn">Show more</div>
<div class="div_with_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue
lectus sed tortor placerat quis porttitor dui vehicula. Morbi molestie
tortor sit amet eros tempor consectetur.
</div>
<div class="show_more_btn">Show more</div>
<div class="div_with_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue
lectus sed tortor placerat quis porttitor dui vehicula. Morbi molestie
tortor sit amet eros tempor consectetur.
</div>
<div class="show_more_btn">Show more</div>
CSS Code:
.showMore
{
display:block;
}
.div_with_content
{
display:none;
}
So i got plenty of divs with class "div_with_content"(witch are not displayed) with some content, usually text. Below each "div_with_content" there is a button wrapped in div "show_more_btn". So when i press on div show_more_btn there will be style added (trought addClass in jQuery) of class showMore to show this div slowly. So its easy to achieve:
JS CODE:
$(function() {
$(".show_more_btn").click(function(event){
$(".div_with_content").addClass("showMore").show(1500,"swing");
});
});
But there is only one problem: when I press whichever show_more_btn all div_with_content are slowly shown, not only one precisely over show_more_btn. So what i want: when i press "show_more_btn" only "div_with_content" precisly over "show_more_btn" is going to be shown others "div_with_content" are still not visible. How do I achieve that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需要修复您的 JavaScript 代码,使其仅适用于您想要的
div_with_content
,而不是全部。试试这个:You just need to fix your javascript code so that it will only apply to the
div_with_content
that you want, and not all of them. Try this:http://api.jquery.com/prev/
您正在寻找 prev() 方法jquery。它也将采用一个选择器,并且只会选择单击的类之前的类。
沿着这些思路的东西。
http://api.jquery.com/prev/
you are looking for the prev() method of jquery. it will take a selector as well and will only select the class that was previous to the one clicked.
something along those lines.
您可以使用
prev
http://api.jquery.com/prev/You can use
prev
http://api.jquery.com/prev/