Structure:
<div id= "box" > <input type= "button" value= "Books" class= "on" > <input type= "button" value= "Picture" > <input type= "button" value= "column" > <div style= "Display:block;" > Books </div> <div> pictures </div> <div> columns </div></div>
Style:
#box div{ width:200px; height:200px; Background: #ccc; Display:none; } . on{ color: #fff; Background:green; }
The idea of the tab is that when we click on the current button, we'll clear all the styles and add styles to the current buttons and boxes.
<script> //Get element var OBox = document.getElementById ("box"); var abtn = obox.getelementsbytagname ("input"); var adiv = obox.getelementsbytagname ("div"); ABTN is a set of elements, so you need to use the For loop for (Var i=0;i<abtn.length;i++) { abtn[i].index=i; Abtn[i].onclick=function () { for (var k=0;k<abtn.length;k++) {//First empty all styles abtn[k].classname = '; Adiv[k].style.display = ' none '; } Add a style to the current button and div this.classname = ' on '; Adiv[this.index].style.display = ' block '; }; } </script>
Relevant knowledge points:
The Index property returns the indexed position of the selected item in the drop-down list
Syntax: Optionobject.index
The ClassName property sets or returns the class property of the element.
Object.classname=classname
JS Native Implementation tab function