Select and add class in javascript

前端 未结 5 1145
盖世英雄少女心
盖世英雄少女心 2021-02-05 10:12

Cross Platform if possible, how can I select classes in Javascript (but not Jquery please -MooTools is fine though-) on code that I can\'t add an ID? Specifically, I want to add

5条回答
  •  情话喂你
    2021-02-05 10:42

    I know this is old, but is there any reason not to simply do this (besides potential browser support issues)?

    document.querySelectorAll("li.even, li.odd").forEach((el) => {
        el.classList.add('cf');
    });
    

    Support: https://caniuse.com/#feat=es5

提交回复
热议问题