js中HTMLCollection如何循环

js中HTMLCollection如何循环_第1张图片

//不带索引
let divCon = document.getElementsByClassName("el-form-item__error");
if (divCon.length > 0) {
    for (var item of divCon) {
      console.log("打印:", item.innerText);
    }
}
//带有索引
let divCon = document.getElementsByClassName("el-form-item__error");
if (divCon.length > 0) {
  for (var i = 0; i < divCon.length; i++) {
     console.log("11", divCon[i].innerText); i是索引值
  }
}

你可能感兴趣的:(javascript,vue.js,前端)