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

后端 未结 9 1231
情深已故
情深已故 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 07:59

    Here is ES6 version of solution

    const classToggle = (el, ...args) => args.map(e => el.classList.toggle(e))
    

    const classToggle = (el, ...args) => {
      args.map(e => el.classList.toggle(e))
    }
    .a {
      color: red
    }
    
    .b {
      background: black
    }
    
    .c {
      border-color: yellow
    }

    And here's old JS code:

    var classToggle = function classToggle(el) {
      for (
        var _len = arguments.length,
          args = new Array(_len > 1 ? _len - 1 : 0),
          _key = 1;
        _key < _len;
        _key++
      ) {
        args[_key - 1] = arguments[_key];
      }
    
      args.map(function (e) {
        return el.classList.toggle(e);
      });
    };
    

提交回复
热议问题