简体   繁体   English

如何使用点击功能jQuery仅隐藏同一类中的元素

[英]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>

A functional example 功能实例

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();
  });
});

JSBIN JSBIN

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.

相关问题 对多个元素使用相同的单击功能(jQuery) - Using same click function for multiple elements (Jquery) 如何使用复选框隐藏函数内的元素? - How to hide elements inside function using checkbox? 单击时隐藏/显示具有相同类的元素 - Hide/show elements with same class on click 如何使用jQuery隐藏表格元素并隐藏引导程序类 - How to hide table elements using jquery and hide class of bootstrap 如何在单击元素时切换类,但使用 JQuery 从所有其他元素中删除相同的类? - How do I toggle a class on click of an element but remove the same class from all other elements using JQuery? 显示/隐藏或切换同一类的多个元素,一次单击仅显示一个 - Show/Hide OR Toggle multiple elements with same class, only show one at a time on click 如何使用jQuery在父div中隐藏任何子元素 - How to hide any child elements inside a parent div using jquery 使用jquery按类隐藏同一父项内的元素 - Hide element inside same parent by class with jquery 如何使用jquery或javascript计算类相同的html元素,但在特定的内部<ul> - How do I count html elements whose class is the same using jquery or javascript, but inside a particular <ul> 如何使用类名仅在一个元素上运行jquery函数。 有更多的元素使用相同的类 - how to run jquery function only on one element using class name. there is more element using same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM