【发布时间】:2011-11-06 08:14:59
【问题描述】:
我无法显示和隐藏一些 div。 我唯一拥有的是一个班级的跨度。我们需要点击的地方,显示或隐藏父级h2下方的div 我无法在 h2 或 div 中添加一些类(这来自一些 cms,我们无法添加 css 类),所以我们需要使用一些 jquery 来完成。
<div>
<h2>
<span class="h2toggle">Heading (This shows an hide the div below)</span>
</h2>
<div>
<p>Some text</p>
<p>More text</p>
</div>
</div>
<div>
<h2>
<span class="h2toggle">Heading 2 (This shows an hide the div below)</span>
</h2>
<div>
<p>Some text 2</p>
<p>More text 2</p>
</div>
</div>
现在我有类似下面的代码。 但是现在,页面将被加载。我们将隐藏“一些文本”和“更多文本”周围的 div。 但是你可以看到它并不是从一开始就隐藏的。它在向上滑动。
它应该从一开始就被隐藏(带有文本的 div )。没有看到页面有任何变化。 加载页面后,必须可以切换标题。
(function ($) {
$(function () {
$('.h2toggle').each(function () {
$(this).parent('h2').addClass('h2toggle');
$(this).removeClass('h2toggle');
$(this).parent().siblings(':not(h2):visible').slideUp()
});
$('.h2toggle').click(function () {
$(this).siblings(':not(h2):visible').slideUp()
$(this).siblings(':not(h2):hidden').slideDown()
});
});
})(jQuery);
【问题讨论】:
-
第一眼之后......你错过了很多分号 '** ; **'
-
@roXon:但是在 JS 中不需要。 ;-)
-
布拉德...谢谢!我知道它不会引发令人作呕的错误。但是……啊,随便。 ;)
标签: jquery asp.net html css vb.net