[英]How to hide only the elements inside with the same class using click function, jQuery
I have 3 elements with the same classes, I want to click on the button "promo__main--details" and hide all content inside the <article>
tag and show the <img>
tag, but with my code it hides the same elements in the others <tags>
, I was trying to use this
method but it didn't work. 我有3个具有相同类的元素,我想单击按钮“ promo__main--details”并将所有内容隐藏在
<article>
标记内并显示<img>
标记,但是对于我的代码,它隐藏了相同的元素其他<tags>
,我试图使用this
方法,但是没有用。
Here's my code. 这是我的代码。
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space">
<article class="promotions__item promo__one">
<h2 class="promo__main--title">Esqui sin límites <br> en Colorado</h2>
<h6 class="promo__main--subtitle">Epic Pass</h6>
<span class="promo__main--details">Ver Más</span>
<img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height:"auto">
</article>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space">
<article class="promotions__item promo__two">
<h2 class="promo__main--title">¡Promoción de Hospedaje <br> en Clásico Vail!</h2>
<h6 class="promo__main--subtitle invisible">Epic Pass</h6>
<span class="promo__main--details">Ver Más</span>
<img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height:"auto">
</article>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space">
<article class="promotions__item promo__three">
<h2 class="promo__main--title">Tus fotos navideñas <br> en Canadá</h2>
<h6 class="promo__main--subtitle">Mont Tremblant</h6>
<span class="promo__main--details">Ver Más</span>
<img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height:"auto">
</article>
</div>
And the JS: 和JS:
$(".promo__main--details").click(function(){
$(".promo__main--title", this).hide();
$(".promo__main--subtitle", this).hide();
$(".promo__main--details", this).hide();
$(".promo__main", this).css({"padding":"0px", "min-height" : "auto"});
$(".promo__main img", this).show();
});
Hope you guys can help me, Thanks! 希望你们能帮助我,谢谢!
JavaScript's this
keyword refers to the clicked element. JavaScript的
this
关键字指的是clicked元素。 But it seems that you want to select elements within the parent <article>
. 但是似乎您想在父
<article>
选择元素。
I suggest using jQuery's parent()
to traverse from the clicked element to its parent <article>
. 我建议使用jQuery的
parent()
从clicked元素遍历到其父<article>
。
$(".promo__main--details").click(function() { $article = jQuery(this).parent('article'); $(".promo__main--title,.promo__main--subtitle,.promo__main--details", $article).hide(); $("img", $article).show(); });
article img { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space"> <article class="promotions__item promo__one"> <h2 class="promo__main--title">Esqui sin límites <br> en Colorado</h2> <h6 class="promo__main--subtitle">Epic Pass</h6> <span class="promo__main--details">Ver Más</span> <img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height: "auto"> </article> </div> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space"> <article class="promotions__item promo__two"> <h2 class="promo__main--title">¡Promoción de Hospedaje <br> en Clásico Vail!</h2> <h6 class="promo__main--subtitle invisible">Epic Pass</h6> <span class="promo__main--details">Ver Más</span> <img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height: "auto"> </article> </div> <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space"> <article class="promotions__item promo__three"> <h2 class="promo__main--title">Tus fotos navideñas <br> en Canadá</h2> <h6 class="promo__main--subtitle">Mont Tremblant</h6> <span class="promo__main--details">Ver Más</span> <img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height: "auto"> </article> </div>
I think that this is more easy 我认为这更容易
jQuery jQuery的
$(".promo__main--details").click(function(){
$(this).closest('.content').hide();
$(this).closest('article').children('.image').show();
});
CSS CSS
article .image {
display:none;
}
HTML HTML
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space">
<article class="promotions__item promo__one">
<div class="content">
<h2 class="promo__main--title">Esqui sin límites <br> en Colorado</h2>
<h6 class="promo__main--subtitle">Epic Pass</h6>
<span class="promo__main--details">Ver Más</span>
</div>
<div class="image">
<img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height:"auto">
</div>
</article>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space">
<article class="promotions__item promo__two">
<div class="content">
<h2 class="promo__main--title">¡Promoción de Hospedaje <br> en Clásico Vail!</h2>
<h6 class="promo__main--subtitle invisible">Epic Pass</h6>
<span class="promo__main--details">Ver Más</span>
</div>
<div class="image">
<img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height:"auto">
</div>
</article>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 no-space">
<article class="promotions__item promo__three">
<div class="content">
<h2 class="promo__main--title">Tus fotos navideñas <br> en Canadá</h2>
<h6 class="promo__main--subtitle">Mont Tremblant</h6>
<span class="promo__main--details">Ver Más</span>
</div>
<div class="image">
<img src="images/promociones/promo2-2.jpg" alt="Promociones" width="100%" height:"auto">
</div>
</article>
</div>
You can do this with a lot less code: 您可以用更少的代码来做到这一点:
$(function(){
$("body").on("click", "button.promo__main--details", function(){
$(this).closest("article").find("*:not(button)").toggle();
});
});
I made the assumption that you you wanted to hide everything except the button. 我假设您要隐藏除按钮以外的所有内容。 Also, passed the context to the
$
is pretty old fashioned. 而且,将上下文传递给
$
是相当老式的。 find()
is a new method. find()
是一个新方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.