Js and jQuery simply implements TAB functionality

  • 2020-03-30 01:10:58
  • OfStack

The first method is to use native js code as follows:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> Simple TAB </title>
    <style type="text/css">
    body,ul,li{margin:0;padding:0;}
    body{font:12px/1.5 Tahoma;}
    #outer{width:450px;margin:10px auto;}
    #tab{overflow:hidden;zoom:1;background:#000;border:1px solid #000;}
    #tab li{float:left;color:#fff;height:30px;cursor:pointer;line-height:30px;list-style-type:none;padding:0 20px;}
    #tab li.current{color:#000;background:#ccc;}
    #content{border:1px solid #000;border-top-width:0;}
    #content ul{line-height:25px;display:none;margin:0 30px;padding:10px 0;}
    </style>
    <script type="text/javascript">
      window.onload = function ()
      {
          var oLi = document.getElementById("tab").getElementsByTagName("li");
          var oUl = document.getElementById("content").getElementsByTagName("ul");
          for(var i = 0; i < oLi.length; i++)
          {
              oLi[i].index = i;
              oLi[i].onmouseover = function (){
                  for(var n = 0;n < oLi.length;n++)
                  {
                      oLi[n].className = "";
                      this.className = "current";
                  }
                      for(var n = 0;n <oUl.length;n++)
                      {
                          oUl[n].style.display = "none";
                          oUl[this.index].style.display="block";
                      }
              }
          }
      }
    </script>
    </head>
<body>
<div id="outer">
<ul id="tab">
    <li class="current"> Science and technology </li>
    <li> tourism </li>
    <li> entertainment </li>
</ul>
<div id="content">
    <ul style="display:block;">
        <li> Macro � R7 This test: the trackpad design is too alternative ( figure )</li>
        <li>  Ultra-thin and waterproof   SONY Xperia Tablet Z Tablet review </li>
        <li> nokia Lumia 925 review   One of the best WP8 A smart phone </li>
        <li>  lenovo IdeaPad S405 review   Performance mainstream endurance to be improved </li>
        <li> Hot pepper M2 Smartphone reviews :  We'll be old if we don't </li>
        <li> lenovo K900 The internal structure   See homebred artifact how many home - made goods ( This Mosaic )</li>
        <li> lenovo K900 The internal structure   See homebred artifact how many home - made goods ( This Mosaic )</li>
        <li> Web peel effect </li>
        <li> lenovo K900 The internal structure   See homebred artifact how many home - made goods ( This Mosaic )</li>
    </ul>
    <ul>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
    </ul>
    <ul>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
    </ul>
</div>
</div>
</body>
</html>

The second method is implemented with jQuery, and the code is simpler than the first

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> Simple TAB </title>
    <style type="text/css">
    body,ul,li{margin:0;padding:0;}
    body{font:12px/1.5 Tahoma;}
    #outer{width:450px;margin:10px auto;}
    #tab{overflow:hidden;zoom:1;background:#000;border:1px solid #000;}
    #tab li{float:left;color:#fff;height:30px;cursor:pointer;line-height:30px;list-style-type:none;padding:0 20px;}
    #tab li.current{color:#000;background:#ccc;}
    #content{border:1px solid #000;border-top-width:0;}
    #content ul{line-height:25px;display:none;margin:0 30px;padding:10px 0;}
    </style>
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">

    $(function(){
            var timeid;
          $("#tab").find("li").each(function(index){
              var sLi=$(this);
              sLi.mouseenter(function(){
                  timeid= setTimeout(function(){
                      sLi.addClass("current").siblings().removeClass("current");
                      sLi.parent().next().find("ul:eq(" + index +")").show().siblings().hide() ;
                 },300);
              }).mouseleave(function(){
                     clearTimeout(timeid);
                      })
          })
        })
    </script>
    </head>
<body>
<div id="outer">
<ul id="tab">
    <li class="current"> Science and technology </li>
    <li> tourism </li>
    <li> entertainment </li>
</ul>
<div id="content">
    <ul style="display:block;">
        <li> Macro � R7 This test: the trackpad design is too alternative ( figure )</li>
        <li>  Ultra-thin and waterproof   SONY Xperia Tablet Z Tablet review </li>
        <li> nokia Lumia 925 review   One of the best WP8 A smart phone </li>
        <li>  lenovo IdeaPad S405 review   Performance mainstream endurance to be improved </li>
        <li> Hot pepper M2 Smartphone reviews :  We'll be old if we don't </li>
        <li> lenovo K900 The internal structure   See homebred artifact how many home - made goods ( This Mosaic )</li>
        <li> lenovo K900 The internal structure   See homebred artifact how many home - made goods ( This Mosaic )</li>
        <li> Web peel effect </li>
        <li> lenovo K900 The internal structure   See homebred artifact how many home - made goods ( This Mosaic )</li>
    </ul>
    <ul>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
        <li> Fall in love with Seattle, the United States panoramic tour selection recommended  </li>
    </ul>
    <ul>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
        <li> Ang lee to quit us TV drama tyrant: need a break </li>
    </ul>
</div>
</div>
</body>
</html>

The first way to use native js is to borrow the method with others, the second is I wrote on the basis of the first method...


Related articles: