开发者

List of hidden divs with buttons to show-hide each one

开发者 https://www.devze.com 2023-03-17 09:34 出处:网络
does anyone know of a simple script to have a list of items and show/hide one <li> at a time without engaging ALL of them in the list? Just seeing if anyone had something clever.

does anyone know of a simple script to have a list of items and show/hide one <li> at a time without engaging ALL of them in the list? Just seeing if anyone had something clever.

List of hidden divs with buttons to show-hide each one

I want to find the parent of each link in a list so it only slideToggle's the appropriate <li> as you go down. I'm trying to list classes for a school and you can open them and see more as you go along, but not open ALL of them at once, and without writing 200 click statements ;)

$('.link').click(function() {
  $('.li').slideTog开发者_C百科gle();
});


$(this).parent().parent().next('.toggle').toggle('slow')
or

$(this).closest('tr').next('.toggle').toggle('slow');


Why not using this:

$('.link').click(function(){
    $(this).parents('.li').slideToggle();
});

To explain, in an event handler you have access to the element that triggered the event via this.

From there, all you need to do is place that element via this in a jQuery object $(this), and use jQuery's traversal methods to locate the element you're targeting.

Also, please note that '.li' will look for an element that has the class named li applied to it.

If you were actually targeting the <li> elements irrespective of their class, then you'd want to remove the ..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号