Simple Realization JS tab switch effect _javascript skill

Source: Internet
Author: User

This article introduces the implementation of JS tab Switching effect of the detailed code, share for everyone to reference, the specific content as follows

Ideas:

    • 1, get the element;
    • 2, for loop button element add onclick (click) or onmousemove (move in) event;
    • 3, click the current button will be highlighted in the state of the display, through the For loop calendar all the button style set to NULL in the all div display set to none.
    • 4, click the current button to add style, the current Div displayed, display set to block.

HTML code:

 <div id= "Div1" >
  <input type= "button" class= "Active" value= "1"/> <input type=
  "button" value= "2"/>
  <input type= "button" value= "3"/> <input "
  button" type= "4" value=/>
   class = "Div2" style= "Display:block"; >11</div>
   <div class= "Div2" >22</div>
   <div class= "Div2" >33</div>
   <div class= "Div2" >44</div>
 </div>
 

CSS style:

 . Active
  {
  background: #9CC;
  }
 . Div2
  {
  width:300px;
  height:200px;
  border:1px #666666 solid;
  Display:none;
  }

JS Code:

<script>
window.onload=function () {
 
  var Odiv=document.getelementbyid (' Div1 ');//Get Div
  var btn =odiv.getelementsbytagname (' input '); get the input
  var div2=odiv.getelementsbytagname (' div ') under the DIV;//get div under div For
 
 (i=0;i<btn.length;i++)/loop each button
  {
   btn[i].index=i//btn[i] is the first button of the specified button; Adds an index property to the button and sets the value of index to I
   btn[i].onclick=function ()// button of the first click event
  {for
  (i=0;i<btn.length;i++)//Loop Remove button style, the div hide
   {
    btn[i].classname= '/ Empty button style
    div2[i].style.display= ' none '//Hide div
   }
    this.classname= ' active '//Self add active
    div2[ this.index].style.display= ' block '//this.index is current div}}}
</script>

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.