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

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

    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');
    

提交回复
热议问题