jQuery slide一次切换一个div,而不是全部独立 [英] jQuery slideToggle one div at a time instead of all independently
问题描述
我正在使用下面的用于切换div的函数,通过它,任何一个条目内容div都可以独立于所有打开或关闭.
I'm using the function below that toggles divs, and with it, any one of the entry-content divs can be opened or closed independently of all.
如果在任何时候都只打开一个条目内容div,那就太好了.单击关闭的条目标题div将关闭任何其他条目内容div,然后打开一个单击的div.我需要保留html标记,因为它是由Wordpress动态创建的.这可能吗?
What would be great is if only one entry-content div would be open at any time. Clicking a closed entry-title div would close any other entry-content div and then open the one clicked. I need to stay with the html markup, as it is created dynamically by Wordpress. Is this possible?
功能
jQuery(document).ready(function($) {
$(".entry-content").hide('slow');
$(".entry-title").click(function() {
$(this).parent().children(".entry-content").slideToggle(500); });
});
html
<div class="entry-post">
<h2 class="entry-title">Post Title 1</h2>
<div class="entry-content">Lorem Ipsum...
</div></div>
<h2 class="entry-title">Post Title 2</h2>
<div class="entry-content">Lorem Ipsum...
</div></div>
More of the same....
</div>
推荐答案
每次单击一次,只需将它们全部关闭:
Just close them all up again every time one is clicked:
jQuery(document).ready(function($) {
$(".entry-content").hide('slow');
$(".entry-title").click(function() {
$(".entry-content").hide();
$(this).parent().children(".entry-content").slideToggle(500); });
});
示例: http://jsfiddle.net/auUxk/
这篇关于jQuery slide一次切换一个div,而不是全部独立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!