选项卡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="关键字,关键词">
<meta name="Description" content="描述和简介">
<title>Title</title>
<style type="text/css">
*{margin:0;padding:0;}
body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
a{text-decoration:none;}
img{border:none;}
ol,ul{list-style:none;}
#box{
position: relative;
width: 360px;
height: 580px;
margin : 50px auto;
}
#box .pic{
float: left;
position: relative;
width: 325px;
height: 100%;
}
#box .pic img{
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#box .pic img.show{
display: block;
}
#box .txt{
position: absolute;
bottom: 0;
left: 0;
width: 325px;
height: 30px;
background: rgba(0,0,0,0.5);
text-align: center;
line-height: 30px;
color: #fff;
font-size: 14px;
}
#box .tab{
float: left;
width: 30px;
}
#box .tab ul li{
width: 30px;
height: 30px;
margin-bottom: 2px;
background: #000;
line-height: 30px;
text-align: center;
font-size: 16px;
font-weight: bold;
cursor: pointer;
color: #fff;
}
#box .tab ul li.active{
background: #ff305c;
}
#box .tab ul li.aa{
border: 5px solid yellowgreen;
}
</style>
</head>
<body>
<div id="box">
<div class="pic">
<img class="show" src="images/g1.jpg" alt="">
<img src="images/g2.jpg" alt="">
<img src="images/g3.jpg" alt="">
<img src="images/g4.jpg" alt="">
</div>
<p class="txt">可爱萌狗狗</p>
<div class="tab">
<ul>
<li class="active aa">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
</div>
<script>
var aImg = document.querySelectorAll("#box .pic img"),
aTab = document.querySelectorAll("#box .tab ul li"),
oTxt = document.querySelector("#box .txt"),
pArrTxt = ["可爱萌狗狗","萌萌小猫咪","呆萌小猫咪","愤怒小翠鸟"],
length = aImg.length,
index = 0;
for(var i = 0; i < length; i++){
aTab[i].ind = i;
aTab[i].onclick = function (){
aImg[index].className = "";
aTab[index].className = "";
index = this.ind;
aImg[index].className = "show";
aTab[index].className = "active";
oTxt.innerHTML = pArrTxt[index];
};
};
</script>
</body>
</html>
添加类名(需要查重)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="关键字,关键词">
<meta name="Description" content="描述和简介">
<title>Title</title>
<style type="text/css">
*{margin:0;padding:0;}
body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
a{text-decoration:none;}
img{border:none;}
ol,ul{list-style:none;}
</style>
</head>
<body>
<div id="box" class="on on aa wrap dachui aa"></div>
<script>
var oBox = document.getElementById("box");
addClass(oBox, "on on dachui on dachui wrap wrap on wrap aa dachui");
function addClass (ele , cName){
var arr1 = ele.className.split(" ");
var arr2 = cName.split(" ");
var arr3 = arr1.concat(arr2);
console.log(arr3);
for(var i = 0; i < arr3.length; i++ ){
for(var j = arr3.length-1; j > i; j--){
if(arr3[i] == arr3[j]){
arr3.splice(j,1);
};
};
};
ele.className = arr3.join(" ");
console.log(arr3);
};
</script>
</body>
</html>
移除类名(不需要查重)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="关键字,关键词">
<meta name="Description" content="描述和简介">
<title>Title</title>
<style type="text/css">
*{margin:0;padding:0;}
body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
a{text-decoration:none;}
img{border:none;}
ol,ul{list-style:none;}
</style>
</head>
<body>
<div id="box" class="on wrap wrap aa dachui wrap dachui bb cc"></div>
<script>
var oBox = document.querySelector("#box");
removeClass(oBox,"on dachui wrap bb");
function removeClass(ele,cName){
var arr1 = ele.className.split(" ");
var arr2 = cName.split(" ");
console.log(arr1);
console.log(arr2);
for(var i = 0; i < arr2.length; i++){
for(var j = arr1.length-1; j >= 0; j--){
if(arr2[i] == arr1[j]){
arr1.splice(j,1);
}
};
};
ele.className = arr1.join(" ");
};
</script>
</body>
</html>