parent().children() 和切换不起作用

发布于 2024-12-15 16:46:46 字数 1952 浏览 4 评论 0 原文

我有以下 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' 类)。这是为什么?

I have the following 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> 

And the following 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; }

With the following 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');
});

});

What happens is the entire div disappears. When I take out the $(this).toggleClass('hide'); code, the collapse div properly collapses (but the 'hide' class which shows a closed triangle image isn't applied, obviously). Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪梦之恋 2024-12-22 16:46:46

$(this).toggleClass('hide') 行将

更改为

因此,正确的 CSS 选择器应该是:

.connections h2.hide {}
.connections h2.hide.over {}

工作示例: http://jsfiddle.net/PTnXU/

您的原始内容选择器 .connections .hide h2.over 如果在

,例如:

<div class="connections">
  <div class="hide">
    <h2 class="over"></h2>
  </div>
</div>

The line $(this).toggleClass('hide') changes <h2></h2> to <h2 class="hide"></h2>.

Therefore, the correct CSS selectors should be:

.connections h2.hide {}
.connections h2.hide.over {}

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:

<div class="connections">
  <div class="hide">
    <h2 class="over"></h2>
  </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文