📌  相关文章
📜  javascript 将类添加到所有子元素 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:24.784000             🧑  作者: Mango

代码示例1
// Add class 'hide' to every child div on 1st child lvl 
var el = document.getElementById('your-element-id');
var classList = 'classList' in el;
for (var i = 0; i < el.children.length; i++) {
  var child = el.children[i];
  if (child.tagName == 'DIV') {
    if (classList) {      
        child.classList.add('hide');                                  
    } else {
      child.className += ' hide'
    }}}