Hiding all elements with the same class name?

前端 未结 5 1266
一生所求
一生所求 2020-12-17 08:38

I\'m trying to hide elements with the same class name (float_form), but I\'m also trying to use the script below to show them (all of the float_form class divs are initially

5条回答
  •  感动是毒
    2020-12-17 08:52

    If you're looking into jQuery, then it's good to know that you can use a class selector inside the parameters of $ and call the method .hide().

    $('.myClass').hide(); // all elements with the class myClass will hide.
    

    But if it's a toggle you're looking for, use .toggle();

    But here's my take on a good toggle without using jQuery:

    function toggle( selector ) {
      var nodes = document.querySelectorAll( selector ),
          node,
          styleProperty = function(a, b) {
            return window.getComputedStyle ? window.getComputedStyle(a).getPropertyValue(b) : a.currentStyle[b];
          };
    
      [].forEach.call(nodes, function( a, b ) {
        node = a;
    
        node.style.display = styleProperty(node, 'display') === 'block' ? 'none' : 'block';
      });
    
    }
    
    toggle( '.myClass' );
    

    Demo here (Click "Render" to run): http://jsbin.com/ofusad/2/edit#javascript,html

提交回复
热议问题