オリジナルJSでtab切替効果を実現
2995 ワード
効果ページjsbinコード
Document
-
-
-
- 
- 
- 
function $(str) {
return document.querySelector(str);
}
var tabHeader = $(".tab-header>ul");
var tabCt = $(".tab-content>ul")
tabHeader.addEventListener("click", function(e) {
if (e.target.tagName.toLowerCase() === "li") {
for (var i = 0; i < tabHeader.children.length; i++) {
tabHeader.children[i].classList.remove("oncls");
}
e.target.classList.add("oncls");
for (var i = 0; i < tabCt.children.length; i++) {
tabCt.children[i].classList.remove("see");
}
var index = [].indexOf.call(tabHeader.children, e.target);
tabCt.children[index].classList.add("see");
}
})