【问题标题】:Target only the div directly after a div in jQuery仅在 jQuery 中的 div 之后直接定位 div
【发布时间】:2017-02-12 07:04:48
【问题描述】:

您好,我想在 jQuery 中直接在 div 之后定位一个 div。

这是我的 HTML 结构:

<!-- 1 -->
<div class="title">
    <p>title</p>
</div>

<div class="boxes">
    <div class="box"></div>
    <div class="box"></div>
</div>

<!-- 2 -->
<div class="title">
    <p>title</p>
</div>

<div class="boxes">
    <div class="box"></div>
    <div class="box"></div>
</div>

<!-- 3 -->
<div class="title">
    <p>title</p>
</div>

<div class="boxes">
    <div class="box"></div>
    <div class="box"></div>
</div>

所以从上面的代码中,我希望逻辑是这样的:

“当用户点击一个 'title' 类的 div 时,最接近的 'boxes' div 将向下滑动,而其他 div 不会受到影响”

这是我目前的 jQuery 代码:

jQuery(".title").click(function(){
    jQuery(".boxes").slideToggle();
});

这可行,但当然当您单击“标题”时,“框”滑动切换的所有实例。

注意:我无法更改类或将类添加到 div,因为这些是由 wordpress 自动生成的

有人知道解决办法吗?

谢谢!

【问题讨论】:

  • $(this).next().slideToggle()
  • 哦,我的天哪,这太容易解决了。谢谢你!如果你想要代表点把它作为答案,我会接受它

标签: jquery html


【解决方案1】:

我想你可能想使用 .find()

$(".title").click(function(){
    $(this).find(".boxes").slideToggle();
});

【讨论】:

  • 感谢您的回答!这可能有效,但我已经用他在 cmets 中发布的 Barmars 回答解决了我的问题。不过,我为你的回答投了赞成票
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
相关资源
最近更新 更多