点击时隐藏最近的div(Hide closest div on click)

编程入门 行业动态 更新时间:2024-10-15 06:16:16
点击时隐藏最近的div(Hide closest div on click)

我试图找出如何隐藏我的divs点击,我有两个foreaches所以它将是多个相等的div创建意义相同的类名称和东西所以我想用.closest来隐藏/显示我点击的那个。 如果foreach创建了4个div并且我点击其中一个,我希望那个隐藏/显示。

另外,请参阅以下代码中的注释

@foreach ()) { <div class="vwHoldLiftInfo"> // Bigger div <a class="liftVariTitle">@variants</a><br /> // Click THIS.. <div class="vwSetRepHolder @cssClass"> // To hide THIS.. @foreach ()) { <a>@d.sett x @d.rep @d.kg</a><br /> } </div> </div> }

这是我尝试过的脚本,但它隐藏了所有的div! 可以这样做吗?

$(function() { $(".liftVariTitle").click(function() { $(".vwHoldLiftInfo").children('div').hide(); // .closest/.children? }); });

Im trying to figure out how to hide my divs on click, I have two foreaches so it will be multiple equal divs created meaning same class names and stuff so I figured using .closest to hide/show the one I click. If the foreach creates 4 divs and I click one of them I want that one to hide/show.

Also, see comments in following code

@foreach ()) { <div class="vwHoldLiftInfo"> // Bigger div <a class="liftVariTitle">@variants</a><br /> // Click THIS.. <div class="vwSetRepHolder @cssClass"> // To hide THIS.. @foreach ()) { <a>@d.sett x @d.rep @d.kg</a><br /> } </div> </div> }

This is the script I tried with but it hides all the divs! Can this be done?

$(function() { $(".liftVariTitle").click(function() { $(".vwHoldLiftInfo").children('div').hide(); // .closest/.children? }); });

最满意答案

我只想隐藏最靠近a标签的div )你需要使用$(this)

$(function() { $(".liftVariTitle").click(function() { $(this).closest(".vwHoldLiftInfo").find('.vwRepSetHolder').hide(); // .closest/.children? }); });

(I only want to hide the div thats closest to the a tag) you need to use $(this)

$(function() { $(".liftVariTitle").click(function() { $(this).closest(".vwHoldLiftInfo").find('.vwRepSetHolder').hide(); // .closest/.children? }); });

更多推荐

本文发布于:2023-07-15 07:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Hide   div   click   closest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!