.classList返回一个DOMTokenList。DOMTokenList与数组的工作方式类似,但没有它的所有 等等 很酷的东西.map()。.forEach()todo.classList === 'completed'无法像您希望的那样将 aDOMTokenList与 a进行比较String。todo.classList.value === 'completed'仅当唯一的类todo是completed. 这不应该被使用。todo.classList.contains('completed')如果 will 的至少一类是 ,则将todo起作用completed。这是最好的方法。以下是使用DOMTokenLists 时可能需要的一些技巧:[...todo.classList]; //convert to arraytodo.classList[0]; //unofficial way to get the nth class nametodo.classList.item(0); //standard way to get the nth class name
您的用法不正确。包含可以与类列表一起使用。您不能直接将条件放在类列表方法上。 event.target.classList.add('class3'); event.target.classList.remove('class1'); event.target.classList.contains('class2'); // To check class is present or not it returns boolean value. event.target.classList.toggle('class4');