简体   繁体   中英

DOM error while using .classList.add method

So when I'm trying to add two classes to my elemeny by this code

I have this error

Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('1 cell-red') contains HTML space characters, which are not valid in tokens.

I don't see any incorrect white space in here

What seems to be the issue is that curr_class is a string containing a space

According to the doc https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList

You should add the class one by one.

Note also that what your code does is : it takes the class of an element and adds it to the same element so nothing usefull

It seems like you have a space in your class name: 1 cell-red between 1 and cell-red .

One solution would be to add the classes one by one:

myElement.classList.add('1');
myElement.classList.add('cell-red');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM