简体   繁体   中英

Clicking a Dynamically Generated div that opens another dynamically generated Div

ok, Im going to pass on submitting how I would do this...because I dont want a patch to an otherwise crappy piece of code. Heres what Im trying to do: I use php to output some divs. A corresponding set of divs is generated with the first divs. The whole idea is that when the first div is clicked, the corresponding div slides down. My attempts at this so far have been unsuccessful. Im guessing the id is going to have to correspond with the class of the other div or something to that affect. I've been baking on this one for awhile. Any help is really appreciated!

To start with, go with this structure:

<div class="main" rel="foo">Content</div>
<div class="main" rel="foo2">Content 3</div>
<div class="main" rel="foo3">Content war</div>

<div id="foo">Content</div>
<div id="foo2">Content</div>
<div id="foo3">Content</div>

And now, you can use a simple jQuery:

$(document).ready(function() {
    $('.main').click(function() {
        var opener = $(this).attr('rel');
        $('#' + opener).slideToggle();
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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