Vanilla [removed] Is there a way to toggle multiple CSS-classes in one statement?

后端 未结 9 1270
情深已故
情深已故 2021-01-02 07:50

I use these JavaScript-code to change classes in my script:

var toggleDirection = function() {
  group.classList.toggle(\'left-to-right\');
  group.classLis         


        
9条回答
  •  失恋的感觉
    2021-01-02 08:23

    The following should work; granted that these class-names are defined in your CSS and some elements on the current page have these classNames:

    var toggleDirection = function()
    {
        var ltr, rtl, lst, cls;
    
        ltr = 'left-to-right';
        rtl = 'right-to-left';
        lst = [].slice.call(document.getElementsByClassName(ltr));
    
        lst = ((lst.length > 0) ? lst : [].slice.call(document.getElementsByClassName(rtl)));
    
        lst.forEach
        (
            function(node)
            {
                cls = node.getAttribute('class');
    
                if (cls.indexOf(ltr) > -1)
                { cls.split(ltr).join(rtl); }
                else
                { cls.split(rtl).join(ltr); }
    
                node.setAttribute('class', cls);
            }
        );
    }
    

提交回复
热议问题