One function to toggle multiple hidden elements?

后端 未结 3 582
粉色の甜心
粉色の甜心 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:24

    You would ideally need a way to link the links to the right div. You can do this using the data attributes like this.

    HTML:

    one
    two
    stats one
    stats two

    JS:

    $('.county').click(function() {
        $('#' + $(this).data('divId')).slideToggle();
    });
    

    Example - http://jsfiddle.net/infernalbadger/6wDf9/

    This should be quite easy to do if you are dynamically generating these links.

提交回复
热议问题