首頁  >  文章  >  web前端  >  兩種jQuery實作選項卡功能的方法

兩種jQuery實作選項卡功能的方法

小云云
小云云原創
2018-01-23 10:30:532108瀏覽

實作選項卡有很多方法,但萬變不離其宗,想法很重要,本文主要介紹了jQuery兩種方法寫選項卡的實例,具有很好的參考價值。下面跟著小編一起來看吧,希望能幫助大家。

效果圖:

#程式碼如下:


<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
 <title>JQuery 源码分析</title>
 <style>
 #p1 p{width: 200px;height: 200px;border: 1px solid #FF0000;display: none;}
 .active{background: red;}
 *{margin: 0;padding: 0;}
 .tab:after{content: &#39;&#39;;display: block;clear: both;}
 .tab li{width: 150px;height: 30px;line-height: 30px;text-align: center;cursor: pointer;list-style: none;float: left;margin: 0 10px;background: #ABCDEF;border-radius: 5px;}
 .tab li.active{background: #000;color:#fff;}
 .content:after{content: &#39;&#39;;display: block;clear: both;}
 .content li{width: 460px;height: 300px;padding:20px;background: #f7f7f7;display: none;}
 </style>
 </head>
 <body>
 <p id="p1">
 <input class="active" type="button" value="1" />
 <input type="button" value="2"/>
 <input type="button" value="3"/>
 <p style="display: block;">11111111111</p>
 <p>22222222222</p>
 <p>333333333333</p>
 </p>
 <ul class="tab">
 <li class="active">1</li>
 <li>2</li>
 <li>3</li>
 </ul>
 <ul class="content">
 <li style="display: block;">111111111111</li>
 <li>222222222222</li>
 <li>333333333333</li>
 </ul>
 <script>
 $(function(){
 //jQuery 方法一
 $(&#39;#p1&#39;).find(&#39;input&#39;).click(function(){
 $(&#39;#p1&#39;).find(&#39;input&#39;).attr(&#39;class&#39;,&#39;&#39;);
 $(&#39;#p1&#39;).find(&#39;p&#39;).css(&#39;display&#39;,&#39;none&#39;)
 $(this).attr(&#39;class&#39;,&#39;active&#39;);
 $(&#39;#p1&#39;).find(&#39;p&#39;).eq($(this).index()).css(&#39;display&#39;,&#39;block&#39;);
 });
 //jQuery 方法二
 $(&#39;.tab&#39;).find(&#39;li&#39;).click(function(){
 var index = $(this).index();
 $(this).addClass(&#39;active&#39;).siblings().removeClass(&#39;active&#39;);
 $(&#39;.content&#39;).find(&#39;li&#39;).eq(index).show().siblings().hide();
 })
 })
 </script>
 </body>
</html>

相關推薦:

##jQuery行動裝置Tab選項卡效果實作方法

JavaScript外掛Tab選項卡詳解

#jquery實作tab選項卡切換效果#

以上是兩種jQuery實作選項卡功能的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn