【发布时间】:2015-01-18 01:32:54
【问题描述】:
我有 4 个项目应该可以切换。所以当一个项目可见时,我点击另一个项目,我点击的那个应该打开。另一个应该关闭。
关于如何做到这一点的任何建议?
我尝试将它添加到我的 jQuery 文件中,但没有想要切换的内容......
var sliderContent = $(this).next('.groupf-more');
$('.groupf-more').not(sliderContent).hide();
sliderContent.toggle();
这是我的 jQuery:
jQuery(document).ready(function($) {
$('.groupf-more').hide();
$('.groupf-title').toggle(function() {
$(this).closest('li').find('.groupf-more').slideDown();
$(this, '.toggle-item').removeClass('groupf-title');
$(this, '.toggle-item').addClass('groupf-title-active');
return false;
},
function() {
$(this).closest('li').find('.groupf-more').slideUp();
$(this, '.toggle-item').removeClass('groupf-title-active');
$(this, '.toggle-item').addClass('groupf-title');
return false;
});
});
这是我的 HTML:
<ul class="toggle" style="margin-right: 1%">
<li class="toggle-item">
<div class="groupf-title">Title</div>
<div class="groupf-more">Content</div>
</li>
<li class="toggle-item">
<div class="groupf-title">Title</div>
<div class="groupf-more">Content</div>
</li>
</ul>
【问题讨论】:
-
您使用的是什么版本的 jQuery? jQuery 1.9 中删除了
.toggle的双函数形式。 -
您可以使用活动课程(您已经在做 - 但不要删除 groupf-title,只需添加活动课程作为第二个课程)。当点击一个项目时,你给它一个活跃的类。然后您循环浏览所有项目并关闭所有没有活动类的项目。
-
$(this, '.toggle-item')应该做什么? -
@barmar:我认为 1.9 以上... jQuery 不是我的菜,我一直在测试。除了 div 之间的切换之外,一切都与该脚本一起工作。感谢您到目前为止的回复!
标签: javascript jquery html