One function to toggle multiple hidden elements?

后端 未结 3 578
粉色の甜心
粉色の甜心 2021-01-26 12:55

This is just a snippet but I will have many different links on a page. Each link will have a different hidden element associated with it. I\'m trying to avoid writing 100 diff

3条回答
  •  温柔的废话
    2021-01-26 13:39

    You could use:

    $('.county').click(
        function(){
            var thisIs = $(this).index();
            $('.countystats').eq(thisIs).slideToggle(300);
        });
    

    JS Fiddle demo.

    Note that I removed the br from the html to get the index() accurately. If you need a elements to be one-per-line, use:

    a {
        display: block;
    }
    

    References:

    • eq().
    • index().
    • slideToggle().

提交回复
热议问题