zoukankan      html  css  js  c++  java
  • jquery开发js插件

    1.需要掌握的知识点

      1)(function($){...}(jQuery)):实际上就是匿名函数并且函数用()阔起来,形成闭包,外界对其内部函数没有影响

        $(function(){…});   jQuery(function($) {…});  $(document).ready(function(){…})用法都是一样的,我们自定义插件时需要用到

      2)$.extend(),$.fn.extend()区别

        最主要的特征:$.extend()是扩展的是Jquery类的静态成员

               $.fn.extend()扩展的是Jquery类实例化对象成员

    2.自定义控件分类:带参插件;不带参插件

      1)不带参插件

    <!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>不带参数的jquery插件开发</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            (function($){
                $.fn.extend({
                    myPlugName:function(){
                        $(this).click(function(){
                            alert($(this).val());    
                        });    
                    }
                });    
            })(jQuery);
        </script>
    </head>
    
    <body>
        <input type="button" value="点击我" id="btn" />
    </body>
    
        <script type="text/javascript">
              $("#btn").myPlugName();
        </script>
    </html
    View Code
    方式一:
    (function($){ $.fn.extend({ myPlugName:function(){
    <!--myPlugName你的插件的名字,根据自己的情况来命名--> //dosomethings }); } }); })(jQuery);
    方式二:
    (function($){
         $.fn.myPlugName=function(){<!--myPlugName你的插件的名字,根据自己的情况来命名;$.fn.名字是$的原型添加属性或者方法-->
        //dosomethings
        } })(jQuery);
    $.fn.extend是实例的扩展;$.extend是类的扩展

      2)带参插件

    <!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>无标题文档</title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            (function($){
                $.fn.hilight=function(options){
                    var defaults={
                        foreground:'red',
                        background:'yellow'    
                    };
                    var opts = $.extend(defaults,options);
                    $(this).css("background-color",opts.background);
                    $(this).css("color",opts.foreground);
                };
            })(jQuery);
        </script>
    </head>
    
    <body>
        <div id="myDiv">This is a Params JQuery!</div>
    </body>
    <script type="text/javascript">
        $("#myDiv").hilight({foreground:'blue'});
    </script>
    </html>
    View Code
    方式一:
    (function($){ $.fn.hilight
    =function(options){ var defaults={ foreground:'red', background:'yellow' }; var opts = $.extend(defaults,options);//将defaults与options结合在一起放到$类中作为其成员变量再被赋值给opts在这个区域内做相应操作 //dosomethings }; })(jQuery);
    方式二:
    (function($){
           $.fn.hilight=function(options){
              var defaults={
                foregroup:'red',
                backgroup:'yellow'
              }
            };
            //这里还要将参数放到$中
            var opts=$.extends(defaults,options)
            //dosomethings
        })(jQuery);
  • 相关阅读:
    【BZOJ 4581】【Usaco2016 Open】Field Reduction
    【BZOJ 4582】【Usaco2016 Open】Diamond Collector
    【BZOJ 4580】【Usaco2016 Open】248
    【BZOJ 3754】Tree之最小方差树
    【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版
    【51Nod 1622】【算法马拉松 19C】集合对
    【51Nod 1616】【算法马拉松 19B】最小集合
    【51Nod 1674】【算法马拉松 19A】区间的价值 V2
    【BZOJ 2541】【Vijos 1366】【CTSC 2000】冰原探险
    【BZOJ 1065】【Vijos 1826】【NOI 2008】奥运物流
  • 原文地址:https://www.cnblogs.com/xiaoping1993/p/7056305.html
Copyright © 2011-2022 走看看