parent().children() 和切换不起作用
我有以下 HTML:
<div class="connections">
<h2>Grads that share your interests</h2>
<div id="connections_nav" class="collapse">
<ul>
<li><h3><a href="">Sally Struthers </a></h3><p>Class of 2008</p></li>
<li><h3><a href="">Sally Struthers </a></h3><p>Class of 2008</p>
</li>
<li><h3><a href="">Sally Struthers </a></h3><p>Class of 2008</p>
</li>
</ul>
</div>
</div>
和以下 CSS:
.connections h2 {
background-image: url(images/show.gif);
background-position: 0px;
background-repeat: no-repeat;
color: #660a11;
font-size: 13px;
font-weight: bold;
margin: 0px 0px .4em;
padding: 0px 0px 0px 25px; }
.connections .hide h2 {
background-image: url(images/hide.gif);
background-position: 0px;
background-repeat: no-repeat; }
.connections h2.over {
background-image: url(images/hover-show.gif);
background-position: 0px;
background-repeat: no-repeat; }
.connections .hide h2.over {
background-image: url(images/hover-hide.gif);
background-position: 0px;
background-repeat: no-repeat; }
使用以下 jQuery:
$(document).ready(function() {
$('.connections h2').click(function() {
$(this).parent().children('.collapse').toggle('slow');
$(this).toggleClass('hide');
});
$('.connections h2').hover(
function() {
$(this).toggleClass('over');
},
function () {
$(this).toggleClass('over');
});
});
发生的情况是整个 div 消失了。当我取出 $(this).toggleClass('hide');
代码时,折叠 div 正确折叠(但显然,未应用显示闭合三角形图像的 'hide' 类)。这是为什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$(this).toggleClass('hide')
行将更改为
。
因此,正确的 CSS 选择器应该是:
工作示例: http://jsfiddle.net/PTnXU/
您的原始内容选择器
.connections .hide h2.over
如果在和
,例如:
The line
$(this).toggleClass('hide')
changes<h2></h2>
to<h2 class="hide"></h2>
.Therefore, the correct CSS selectors should be:
Working example: http://jsfiddle.net/PTnXU/
Your original selector,
.connections .hide h2.over
, would have been good if you had another element between the<div>
and the<h2>
, for example: