欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery插件開發(fā)之選項(xiàng)卡制作詳解

 更新時間:2017年08月30日 13:35:02   作者:ghostwu  
這篇文章主要為大家詳細(xì)介紹了jquery插件開發(fā)之選項(xiàng)卡制作,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在jquery中,插件開發(fā)常見的有:

一種是為$函數(shù)本身擴(kuò)展一個方法,這種是靜態(tài)擴(kuò)展(也叫類擴(kuò)展),這種插件一般是工具方法,

還有一種是擴(kuò)展在原型對象$.fn上面的,開發(fā)出來的插件是用在dom元素上面的

一、類級別的擴(kuò)展

$.showMsg = function(){
   alert('hello,welcome to study jquery plugin dev');
  }
  // $.showMsg();

注意要提前引入jquery庫, 上例在$函數(shù)上面添加了一個方法showMsg,那么就可以用$.showMsg()調(diào)用了

$.showName = function(){
   console.log( 'ghostwu' );
  }
  $.showName();

這種插件比較少見,一般都是用來開發(fā)工具方法,如jquery中的$.trim, $.isArray()等等

二、把功能擴(kuò)展在$.fn上,

這種插件就是用在元素上,比如,我擴(kuò)展一個功能,點(diǎn)擊按鈕,顯示當(dāng)前按鈕的值

$.fn.showValue = function(){
  return $(this).val();
}

  $(function(){
   $("input").click(function(){
    // alert($(this).showMsg());
    alert($(this).showMsg());
   });
  });

<input type="button" value="點(diǎn)我">

在$.fn上添加一個showValue方法, 返回當(dāng)前元素的value值. 在獲取到頁面input元素,綁定事件之后,就可以調(diào)用這個方法,顯示按鈕的值 "點(diǎn)我",在實(shí)際插件開發(fā)中,常用的就是這種.接下來,我們就用這種擴(kuò)展機(jī)制開發(fā)一個簡單的選項(xiàng)卡插件:

頁面布局與樣式:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/jquery/1.12.0/jquery.js"></script>
 <style>
  #tab {
   width:400px;
   height:30px;
  }
  #tab li, #tab ul {
   list-style-type:none;
  }
  #tab ul {
   width:400px;
   height: 30px;
   border-bottom:1px solid #ccc;
   line-height: 30px;
  }
  #tab ul li {
   float:left;
   margin-left: 20px;
   padding:0px 10px;
  }
  #tab ul li.active {
   background: yellow;
  }
  #tab ul li a {
   text-decoration: none;
   color:#666;
  }
  #tab div {
   width:400px;
   height:350px;
   background-color:#ccc;
  }
  .clearfix:after{
   content: '';
   display: block;
   clear: both;
   height: 0;
   visibility: hidden;
  }
 </style>
 <script src="tab2.js"></script>
 <script>
  $(function(){
   $("#tab").tabs( { evType : 'mouseover' } );
  });
 </script>
</head>
<body>
 <div id="tab">
  <ul class="clearfix">
   <li><a href="#tab1">選項(xiàng)1</a></li>
   <li><a href="#tab2">選項(xiàng)2</a></li>
   <li><a href="#tab3">選項(xiàng)3</a></li>
  </ul>
  <div id="tab1">作者:ghostwu(1)
   <div>博客: http://www.cnblogs.com/ghostwu/</div>
  </div>
  <div id="tab2">作者:ghostwu(2)
   <div>博客: http://www.cnblogs.com/ghostwu/</div>
  </div>
  <div id="tab3">作者:ghostwu(3)
   <div>博客: http://www.cnblogs.com/ghostwu/</div>
  </div>
 </div>
</body>
</html>

tab2.js文件

;(function ($) {
 $.fn.tabs = function (opt) {
  var def = { evType: "click" }; //定義了一個默認(rèn)配置
  var opts = $.extend({}, def, opt);
  var obj = $(this);

  $("ul li:first", obj).addClass("active");
  obj.children("div").hide();
  obj.children("div").eq(0).show();

  $("ul li", obj).bind(opts.evType, function () {
   $(this).attr("class", "active").siblings("li").attr("class","");
   var id = $(this).find("a").attr("href").substring(1);
   obj.children("div").hide();
   $("#" + id, obj).show();
  });
 };
})(jQuery);

1,一個自執(zhí)行函數(shù),把插件封裝成模塊,把jQuery對象傳給形參$

2,第3行,定義一個默認(rèn)配置,選項(xiàng)卡的觸發(fā)類型,默認(rèn)為點(diǎn)擊事件

3,第4行,如果opt傳參,就用opt的配置,否者就用第3行的默認(rèn)配置,這行的作用就是為了在不改變插件源碼的情況下,可以配置插件的表現(xiàn)形式

4,第7-9行,讓選項(xiàng)卡第一個div顯示,其余的都隱藏,讓第一個tab加上class='active' 黃色高亮選中

5,第11-16行,點(diǎn)擊對應(yīng)的選項(xiàng)卡,讓對應(yīng)的div顯示與隱藏

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論