jQuery Parents()帮助 [英] Jquery parents() help

查看:93
本文介绍了jQuery Parents()帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么这不起作用吗?我正在尝试显示和隐藏内容.

Can someone please tell me why this doesn't work? I'm trying to show and hide content.

这是我的标记

<div class="entry">
  <p class="posted">
    test<br />
    <a href="#" class="toggle" title="Show Comments">
      Show/Hide
    </a>
  </p>
  <div class="box" class="comment">
    test hidden comment
  </div>
</div>


$(function () {
  $('div.box').hide();
  $('a.toggle').click(function () {
    $(this).parents('.entry').next('div.box').toggle(400);
  });
});

此示例现在的放置方式,如果我采用最后一个关闭div并将其移至关闭p标记之后,它可以正常工作,但显示并隐藏所有隐藏内容(divs),即不是我追求的.我只想显示与每个链接相关的内容.

The way this example sits right now, If I take the last closing div and move it up after the closing p tag, it works fine but shows and hides all hidden content (divs) which is not what I'm after. I'd only like to show the content that is associated with each link.

推荐答案

尝试使用.parent而不是.parents.

try use .parent instead of .parents.

这将起作用:

    $(function() {
        $('div.box').hide();
        $('a.toggle').click(function() {
        $(this).parent('.posted').next('div.box').toggle(400);
        });
    });

这篇关于jQuery Parents()帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆