Toggle Class Visibility by Clicking on another Cla

2019-09-02 08:54发布

问题:

I am working on a project and I need to be able to click .node-202 .field-name-field-pin-point and toggle the visibility of .node-202 .group-dealer.

I need both identifiers in the call because there will be multiple nodes, and I do not want all of them opening when I click on one of the pin points.

Edit: Here is an example of the HTML. I am using Drupal so there is a lot of unnecessary code, but it did my best to format it as quickly as I could.

            <article class="node-202 node node-dealer node-teaser contextual-links-region node-by-viewer clearfix">

            <div class="field-group-format field-group-div group-dealer collapsible  speed-fast effect-none"><!-- Begin Class Group Dealer -->
            <h3><span class="field-group-format-toggler">Dealer Container</span></h3>
                            <div class="field-group-format-wrapper" style="display: block;">
                                <div class="field field-name-field-dealer-image field-type-image field-label-hidden"><div class="field-items">
                                <div class="field-item even"><img src="image.jpg" width="280" height="114" alt="" />
                            </div>
                        </div>
                    </div>
            <div class="field field-name-field-dealer-website field-type-link-field field-label-hidden">
            <div class="field-items">
            <div class="field-item even">
            <a href="@" target="_blank">Company Titile</a>
                                </div>
                            </div>
                        </div>
                                    <div class="field field-name-field-dealer-address field-type-text field-label-hidden">
                                    <div class="field-items"><div class="field-item even">81234 Ricardo Court
                                </div>
                            </div>
                        </div>
                                    <div class="field field-name-field-dealer-location field-type-text field-label-hidden">
                                    <div class="field-items"><div class="field-item even">Los Angeles, CA 
                                </div>
                            </div>
                        </div>
                                    <div class="field field-name-field-dealer-country field-type-text field-label-hidden">
                                    <div class="field-items"><div class="field-item even">United States
                                </div>
                            </div>
                        </div>
                                <div class="field field-name-field-dealer-phone-number field-type-text field-label-hidden">
                                <div class="field-items"><div class="field-item even">123-555-2042
                            </div>
                        </div>
                    </div>
                    <div class="field field-name-field-dealer-email field-type-email field-label-hidden">
                        <div class="field-items">
                            <div class="field-item even">
                                <a href="mailto:sales@company.com">sales@company.com</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>  <!-- END CLASS GROUP DEALER -->
            <div class="field field-name-field-pin-point field-type-image field-label-hidden">
                <div class="field-items"><div class="field-item even">
                    <img src="image.png" width="24" height="23" alt="" />
                    </div>
                </div>
            </div>

            </article>

Edit 2: I need to set ".node-202 .group-dealer" to visibility:hidden rather than display:none. It seems that this may be helpful: Equivalent of jQuery .hide() to set visibility: hidden.

回答1:

Assuming that the HTML structure looks like this (and that you have multiple instances of this structure, so you can't just use the class names):

<div class="node-202">
    <div class="field-name-field-pin-point">...</div>
    <div class="group-dealer">...</div>
</div>

you need something like this:

$('.node-202 .field-name-field-pin-point').click(function() {
    $(this).siblings('.group-dealer').toggle();
});

TEST IN JSFIDDLE



回答2:

Try this..

 $('.node-202 .field-name-field-pin-point').bind('click', function(){

            $('.node-202 .group-dealer').toggle();

    })

FIDDLE DEMO