点击时隐藏最近的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? }); });更多推荐
发布评论