How do I change multiple elements with same class name using Javascript?

后端 未结 6 997
无人共我
无人共我 2020-12-21 03:45

Follow up to my previous question - I want to use a button to show / hide multiple elements with the same class name using JS, yet it appears that I can only change the firs

6条回答
  •  星月不相逢
    2020-12-21 04:20

    Easy solution, made together with jQuery. I've provided just one function, which accepts an argument, which is the name of the elements you want to leave in the list.

    var elems = $('.indent').children('div');
    
    function filter(v) {
      elems.filter(function() {
        $(this).show();
        if (!$(this).hasClass(v) && v) {
          $(this).hide();
        }
      });
    }
    .indent {
      margin: .5em 1em .5em 1em;
    }
    
    
    
    
    
    
    • boring IT stuff
    • cool design stuff
    • it stuff and things
    • design stuff
    • it stuff and more
    • more it stuff
    • it stuff

提交回复
热议问题