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

后端 未结 9 1236
情深已故
情深已故 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:15

    For anyone looking for a short answer, you can do this on one line using the rest parameter introduced in ES6/ES2015:

    const toggleCSSclasses = (el, ...cls) => cls.map(cl => el.classList.toggle(cl))
    

    This is pretty close to @attacomsian's answer, but taking advantage of the fact that the rest parameter will return an array - no matter how many arguments is being passed to the function. Which means we can skip the part where we detect whether we're working with a string or an array.

    const toggleCSSclasses = (el, ...cls) => cls.map(cl => el.classList.toggle(cl));
    
    const one = document.querySelector(".one");
    
    one.addEventListener("click", () => {
      toggleCSSclasses(one, "class1");
    });
    
    const two = document.querySelector(".two");
    
    two.addEventListener("click", () => {
      toggleCSSclasses(two, "class1", "class2");
    });
    .class1 { text-decoration: underline }
    .class2 { background: steelblue }
    <p class="one">Click to toggle one class</p>
    <p class="two">Click to toggle two classes</p>

    0 讨论(0)
  • 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);
            }
        );
    }
    
    0 讨论(0)
  • 2021-01-02 08:23

    No it is not possible using Element.classList API directly. Looking at API you can read:

    toggle ( String [, force] ) When only one argument is present: Toggle class value; i.e., if class exists then remove it, if not, then add it. When a second argument is present: If the second argument is true, add specified class value, and if it is false, remove it.

    Reference here.

    You could potentially write your own "utility" function (in vanilla JS) which does what you want, below a very simple demonstrative example which work on top of the classList API:

    var superToggle = function(element, class0, class1) {
      element.classList.toggle(class0);
      element.classList.toggle(class1);
    }
    

    And you call it in this way:

    superToggle(group,'left-to-right', 'right-to-left');
    
    0 讨论(0)
提交回复
热议问题