在 jQuery 中切换重复的类

发布于 2024-09-28 12:31:27 字数 1621 浏览 2 评论 0原文

我有这样的 HTML 代码:

<div id="content">
  <div class="profile_photo">
   <img style="float:left;margin-right:7px;" src="http://gravatar.com/avatar/53566ac91a169b353a78b329bdd35c95?s=50&d=identicon" class="profile_img" alt="{username}"/>
  </div>
  <div class="container" id="status-#">
   <div class="message">
    <span class="username">{username} Debugr Rocks!
   </div>
   <div class="info">24-oct-2010, 14:05 GMT · <a href="#" class="toggle_comment" title="Comment">Comment (5)</a> · <a href="#" title="Flag" class="toggle_flag">Flag</a> · Via <a href="#" title="Twitter">Twitter</a>
</div>
   <div class="comment_container">
    <div class="profile_photo">
     <img style="float:left;margin-right:7px;" src="http://gravatar.com/avatar/53566ac91a169b353a78b329bdd35c95?s=32&d=identicon" class="profile_img" alt="{username}"/>
    </div>
    <div class="comment_message">
     <span class="username"><a href="#" title="{username}">{username}</a></span> Debugr Rocks! XD
    </div>
    <div class="comment_info">24-oct-2010</div>
   </div>
  </div>
  <div class="profile_photo">
   <img style="float:left;margin-right:7px;" src="http://gravatar.com/avatar/53566ac91a169b353a78b329bdd35c95?s=50&d=identicon" class="profile_img" alt="{username}"/>
  </div>

重复两次或多次。我想要做的是,当我单击“评论 (5)”链接时,会出现“comment_container”类,但只有同一“容器”类中的类。

这可能吗?

I have this HTML code:

<div id="content">
  <div class="profile_photo">
   <img style="float:left;margin-right:7px;" src="http://gravatar.com/avatar/53566ac91a169b353a78b329bdd35c95?s=50&d=identicon" class="profile_img" alt="{username}"/>
  </div>
  <div class="container" id="status-#">
   <div class="message">
    <span class="username">{username} Debugr Rocks!
   </div>
   <div class="info">24-oct-2010, 14:05 GMT · <a href="#" class="toggle_comment" title="Comment">Comment (5)</a> · <a href="#" title="Flag" class="toggle_flag">Flag</a> · Via <a href="#" title="Twitter">Twitter</a>
</div>
   <div class="comment_container">
    <div class="profile_photo">
     <img style="float:left;margin-right:7px;" src="http://gravatar.com/avatar/53566ac91a169b353a78b329bdd35c95?s=32&d=identicon" class="profile_img" alt="{username}"/>
    </div>
    <div class="comment_message">
     <span class="username"><a href="#" title="{username}">{username}</a></span> Debugr Rocks! XD
    </div>
    <div class="comment_info">24-oct-2010</div>
   </div>
  </div>
  <div class="profile_photo">
   <img style="float:left;margin-right:7px;" src="http://gravatar.com/avatar/53566ac91a169b353a78b329bdd35c95?s=50&d=identicon" class="profile_img" alt="{username}"/>
  </div>

That is repeated two or more times. What I want to do, is to when I click the "Comments (5)" link, the class "comment_container" appears, but only the one in the same "container" class.

It's this possible?

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

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

发布评论

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

评论(1

一百个冬季 2024-10-05 12:31:27

您可以使用 .closest() 转到 .container 然后 .find() 查看其内部,就像这样:

$(".toggle_comment").click(function() { 
    $(this).closest(".container").find(".comment_container").show();
});

你可以在这里尝试,如果你想找到其他相关的东西到 this 这里是树遍历函数的完整列表。


顺便说一句,您的 HTML 中有一个错误需要更正,

<span class="username">{username} Debugr Rocks! </div>

应该是:

<span class="username">{username} Debugr Rocks! </span>

You can use .closest() to go up to the .container then .find() to look inside it, like this:

$(".toggle_comment").click(function() { 
    $(this).closest(".container").find(".comment_container").show();
});

You can try it here, if you're curious about finding other things relative to this here's a full list of the Tree Traversal functions.


As an aside, there's an error in your HTML that needs correcting, this:

<span class="username">{username} Debugr Rocks! </div>

Should be:

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