赞
踩
获取html dom,然后遍历dom时,报错 tabList.forEach is not a function
var tabList = document.querySelectorAll('.mui-tab-item');
tabList.forEach(function(tabItem){
tabItem.classList.remove('mui-active');
if(tabItem.getAttribute('href') === activeUrl){
tabItem.classList.add('mui-active');
}
})
加上一行代码
tabList = Array.from(tabList);
var tabList = document.querySelectorAll('.mui-tab-item');
tabList = Array.from(tabList);
tabList.forEach(function(tabItem){
tabItem.classList.remove('mui-active');
if(tabItem.getAttribute('href') === activeUrl){
tabItem.classList.add('mui-active');
}
})
Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组。 将类数组对象转换为真正数组