Native JavaScript rewritten tab tab

Source: Internet
Author: User

<!--CSS section--

<style>

*{
margin:0;
padding:0;
}

ul,li{
List-style:none
}
. tabbox{
width:600px;
Clear:both;
Overflow:hidden;
margin:0 Auto;
border:1px solid #ccc;
}
ul.tabnav{
width:600px;
height:30px;
border-bottom:1px solid #ccc;
}

Ul.tabnav li{
width:198px;
height:30px;
line-height:30px;

Float:left;

border-right:1px solid #ccc;
Text-align:center;
}

Ul.tabnav li.active{
color:red;
}

. tabcontent{
width:460px;
Overflow:hidden;;
padding:20px;
}

. tabcontent ul li{
Float:left;
Display:none;
}

. tabcontent ul li.active{
Display:block;
}

</style>

<!--HTML Structure--

<div class= "Tabbox" >
<ul class= "Tabnav" id= "Tabnav" >
<li class= "Item Active" > Language </li>
<li class= "Item" > English </li>
<li class= "Item" > Math </li>
</ul>

<div class= "tabcontent" id= "Tabcont" >
<ul>
<li class= "Item Active" > Chinese content </li>
<li class= "Item" > English content </li>
<li class= "Item" > Math content </li>
</ul>
</div>
</div>

<!--javascript-->

Function Switchtab (tabbtn,tabcontent) {

  var tabnav = document.getElementById (tabbtn);
  var tabcont = document.getElementById (tabcontent);

  This.tabnavli = tabnav.getelementsbytagname (' li ');
  This.tabcontli = tabcont.getelementsbytagname ("Li");

  var _this = this;    //Save the created variable tabs;

  for (var i=0, len=this.tabnavli.length; i<len; i++) {
      This.tabnavli[i].index = i ;
      This.tabnavli[i].onclick = function () {
      _this.tabshow (this);   //This is worth the current click of the option
   };
 }

}

SwitchTab.prototype.tabshow = function (obj) {//obj here is the object that is currently clicked button
For (var i=0, len=this.tabnavli.length; i<len; i++) {
This.tabnavli[i].classname = "";
This.tabcontli[i].classname = "";
}

Obj.classname = "active";
This.tabcontli[obj.index].classname = "active";
};

Window.onload = function () {
var tabs = new Switchtab ("Tabnav", "Tabcont");
};

</script>

The most important thing to rewrite into object-oriented is to be aware of who this refers to. There are two scenarios in which the value of this should be saved.

1. When there is a timer in the object-oriented program

function Aaa () {
THIS.A = 10;
var _this = this;
SetInterval (function () {
_this.show (); If you use this directly, then this point of this is the window
},1000);
}

Aaa.prototype.show = function () {
alert (THIS.A);
};

Window.onload = function () {
var AAA = new AAA ();
};

2. When there are events in the object-oriented program

function Bbb () {
THIS.A = 10;
var _this = this;
var btn = document.getElementById ("btn");

Btn.onclick = function () {
_this.show (); Here this is pointing to the current object
};
}

Bbb.prototype.show = function () {
alert (THIS.A);
};

Window.onload = function () {
var bbb = new BBB ();
};

Native JavaScript rewritten tab tab

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.