简体   繁体   English

在点击时隐藏最接近的div

[英]Hide closest div on click

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. 我试图弄清楚如何在单击时隐藏我的div,我有两个foreach,因此将创建多个相等的div,这意味着相同的类名和内容,因此我想出了使用.closest隐藏/显示我单击的div的方法。 If the foreach creates 4 divs and I click one of them I want that one to hide/show. 如果foreach创建了4个div,然后单击其中一个,则我希望该div隐藏/显示。

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! 这是我尝试过的脚本,但它隐藏了所有div! Can this be done? 能做到吗?

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

( I only want to hide the div thats closest to the a tag ) you need to use $(this) 我只想隐藏最靠近a标签的div ),您需要使用$(this)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM