>  기사  >  웹 프론트엔드  >  수평 및 수직 tabs_javascript 기술 간의 탭 연결을 구현하는 JS 방법

수평 및 수직 tabs_javascript 기술 간의 탭 연결을 구현하는 JS 방법

WBOY
WBOY원래의
2016-05-16 15:37:451303검색

본 글의 예시에서는 JS를 이용하여 가로 탭과 세로 탭 간의 탭 연결을 구현하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 세부 내용은 다음과 같습니다.

카드 내용은 동일하지만, 한 탭을 클릭하면 다른 탭에도 해당 옵션이 표시됩니다. 즉, 둘 다의 기능이 동일하며 이는 매우 영리한 레이아웃 방법입니다.

런닝 효과 스크린샷은 다음과 같습니다.

온라인 데모 주소는 다음과 같습니다.

http://demo.jb51.net/js/2015/js-hx-sx-tab-ld-demo/

구체적인 코드는 다음과 같습니다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>选项卡Tab联动</title>
<style type="text/css">
body{text-align:center;margin:0;padding:0;font-size:12px;}
div,form,img,ul,ol,li,dl,dt,dd{margin:0;padding:0;border:0;}
h1,h2,h3,h4,h5,h6,p,table,td{margin:0;padding:0;font-size:12px;}
li{list-style:none;}
#layout{width:500px;margin:10px auto;}
#tabnav{width:80px;float:left;}
#tabnav li{float:left;width:70px;height:23px;line-height:23px;cursor:pointer;border:1px solid #ccc;margin:2px 0 0 0;display:inline;color:#333;}
#tabnav li.current{color:#f60;font-weight:bold;}
#tab{width:400px;height:235px;float:right;}
#tab ul{width:400px;height:24px;}
#tab li{float:left;width:70px;height:23px;line-height:23px;cursor:pointer;border:1px solid #ccc;margin:0 1px 0 0;display:inline;background:#333;color:#fff;}
#tab li.current{background:#f60;font-weight:bold;}
#tab .tabcon{float:left;width:398px;height:208px;display:none;border:1px solid #ccc;}
#tab .block{display:block;}
</style>
</head>
<body>
<div id="layout">
 <ul id="tabnav">
  <li onclick="go_to(1);" class="current">最新精华帖</li>
  <li onclick="go_to(2);">最新帖子</li>
  <li onclick="go_to(3);">最旧帖子</li>
  <li onclick="go_to(4);">新最帖子</li>
  <li onclick="go_to(5);">旧最帖子</li>
 </ul>
 <div id="tab">
  <ul id="tabnav02">
   <li onclick="go_to(1);" class="current">最新精华帖</li>
   <li onclick="go_to(2);">最新帖子</li>
   <li onclick="go_to(3);">最旧帖子</li>
   <li onclick="go_to(4);">新最帖子</li>
   <li onclick="go_to(5);">旧最帖子</li>
  </ul>
  <div id="tabcon_1" class="tabcon block">
   1
  </div> 
  <div id="tabcon_2" class="tabcon">
   2
  </div> 
  <div id="tabcon_3" class="tabcon">
   3
  </div> 
  <div id="tabcon_4" class="tabcon">
   4
  </div> 
  <div id="tabcon_5" class="tabcon">
   5
  </div>  
  <script type="text/javascript">
  <!--
  var h=document.getElementById("tabnav").getElementsByTagName("li");
  var d=document.getElementById("tabnav02").getElementsByTagName("li");
  function go_to(s){
   for(var i=1;i<=h.length;i++){
    if(s==i){
    h[i-1].className="current";
    d[i-1].className="current";
    document.getElementById("tabcon_"+i).className="tabcon block";
    }
    else{
    h[i-1].className="";
    d[i-1].className="";
    document.getElementById("tabcon_"+i).className="tabcon";
    }
   }
  }
  //-->
  </script>
 </div>
</div>
</body>
</html>

이 기사가 모든 사람의 JavaScript 프로그래밍에 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.