Hiding all elements with the same class name?

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

提交回复
热议问题