Hiding all elements with the same class name?

前端 未结 5 1255
一生所求
一生所求 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:45

    Try :

    function showClass(a){
     var e = [];
     var e = getElementsByClassName(a);
     for(i in e ){
        if(!e[i])return true;
    
        if(e[i].style.display=="none"){
           e[i].style.display="block"
        } else {
           e[i].style.display="none"
        }
     }
     return true;
    }
    

    demo : showClass("float_form");

    0 讨论(0)
  • 2020-12-17 08:48

    vanilla javascript

    function toggle(className, displayState){
        var elements = document.getElementsByClassName(className)
    
        for (var i = 0; i < elements.length; i++){
            elements[i].style.display = displayState;
        }
    }
    
    toggle('float_form', 'block'); // Shows
    toggle('float_form', 'none'); // hides
    

    jQuery:

    $('.float_form').show(); // Shows
    $('.float_form').hide(); // hides
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-17 08:54

    No jQuery needed

    const toggleNone = className => {
      let elements = document.getElementsByClassName(className)
      for (let i = 0; i < elements.length; i++){
        if (elements[i].style.display === "none") {
          elements[i].style.display = "";
        } else {
          elements[i].style.display = "none";
        }
      }
    }
    
    const toggleVisibility = className => {
      let elements = document.getElementsByClassName(className)
      for (let i = 0; i < elements.length; i++){
        let elements = document.getElementsByClassName(className);
        if (elements[i].style.visibility === "hidden") {
          elements[i].style.visibility = "";
        } else {
          elements[i].style.visibility = "hidden";
        }
      }
    }
    
    // run 
    toggleNone('your-class-name-here'); // toggles remove
    // or run 
    toggleVisibility('your-class-name-here'); // toggles hide
    

    Answer provided in ES6 syntax but easily can be converted to ES5 if you wish

    0 讨论(0)
  • 2020-12-17 09:02

    Using jquery

    $(".float_form").each(function(){
        if($(this).css("display") == "none"){
            $(this).show();
        }else{
            $(this).hide();
        }
    });
    
    0 讨论(0)
提交回复
热议问题