Select and add class in javascript

前端 未结 5 1144
盖世英雄少女心
盖世英雄少女心 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:39

    querySelectorAll is supported in IE8, getElementsByClassName is not, which does not get two classes at the same time either. None of them work in iE7, but who cares.

    Then it's just a matter of iterating and adding to the className property.

    var list = document.querySelectorAll(".even, .odd");
    for (var i = list.length; i--;) {
        list[i].className = list[i].className + ' cf';
    }
    

    FIDDLE

提交回复
热议问题