题
我正在使用jQuery使用toggle(),并且我的页面上有一个侧边栏,每个标头为
<h2 class="sidebar-header">
我的代码的一个部分看起来像:
<div class="sidebar-section">
<h2 class="sidebar-header">SUBSCRIBE</h2>
<p class="sidebar">Make sure you subscribe to stay in touch with the latest articles and tutorials. Click on one of the images below:</p>
<div class="rss-icons">
<a href="http://fusion.google.com/add?feedurl=http://feeds.feedburner.com/ryancoughlin"><img src="http://gmodules.com/ig/images/plus_google.gif" width="62" height="17" alt="Add to Google Reader or Homepage" class="feed-image"/></a>
<a href="http://add.my.yahoo.com/rss?url=http://feeds.feedburner.com/ryancoughlin" title="Web/Graphic Design/Development by Ryan Coughlin"><img src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo3.gif" alt="Add feed to My Yahoo" width="62" height="17" class="feed-image" /></a>
<a href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http://feeds.feedburner.com/ryancoughlin" title="Web/Graphic Design/Development by Ryan Coughlin"><img src="http://www.newsgator.com/images/ngsub1.gif" alt="Subscribe in NewsGator Online" class="feed-image" height="17" /></a>
</div>
<hr />
<p class="sidebar feed">Don't have those? Grab the <a href="http://feeds.feedburner.com/ryancoughlin" target="_blank" title="Subscribe to this feed">RSS</a> url.</p>
<p class="sidebar delicious">Make sure you add me to <a href="http://del.icio.us/post?url=http://www.ryancoughlin.com&title=Ryan Coughlin - Web/Graphic Design and Development" target="_blank" title="Add to delicious!">delicious!</a> </p>
</div>
它们各自包裹在那些div元素中。我正在尝试完成它,如果您单击标题,它将收缩内容。我知道Toggle可以做到这一点,但是如果我为每个“侧边栏标头”制作,您将单击页面上的任何一个元素,它将全部隐藏它们,我该怎么做?
解决方案
尝试这个:Nextall
$(".sidebar-header").click(function() {
$(this).nextAll().toggle();
});
其他提示
尝试这样的事情:
假设您有以下代码:
<div class="topdiv">
<h2 class="header">Header 1</h2>
<div class="somecontent">
This is the content
</div>
</div>
<div class="topdiv">
<h2 class="header">Header 2</h2>
<div class="somecontent">
This is the content
</div>
</div>
现在,说当您要单击标头时,该标头的内容会显示 /隐藏:
$(".header").click(function () {
$(this).parent(".topdiv:first").find(".somecontent").toggle();
});
这样,只有特定标头的内容才能被切换,而不是其余的。
现在,您可以分析我为您编写的代码,并在您自己的上下文中应用它。
不隶属于 StackOverflow