jquery 插件编写例子5个

36 篇文章 0 订阅
4 篇文章 0 订阅
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div>
    <a href="http://www.cnblog.com">cnblog</a>
    <a href="http://www.zhihu.com">zhihu</a>
    <a href="http://www.csdn.com">csdn</a>
</div>
<a id='o_a1' href="http://defaultsegment.com">defaultsegment</a>

<section>
    <a href="http://www.cnblog.com">cnblog</a>
    <a href="http://www.zhihu.com">zhihu</a>
    <a href="http://www.csdn.com">csdn</a>
</section>
</body>
<script src="../../jquery_1.11.3.min.js"></script>
<script src="jquery.myplugin.js"></script>
<script>
    $.extend({
        log:function(message){
            var now = new Date(),
            y = now.getFullYear(),
                    m=now.getMonth()+ 1,
                    d=now.getDate(),
                    h=now.getHours(),
                    min = now.getMinutes(),
                    s=now.getSeconds(),
                    time=y+'/'+m+'/'+d+' '+h+':'+min+":"+s;
            console.log(time+' my app:'+message);

        },
        get_my_date:function(message){
            var now= new Date(),
                    y=now.getFullYear(),
                    m=now.getMonth()+ 1,
                    d=now.getDate(),
                    h=now.getHours(),
                    min = now.getMinutes(),
                    s=now.getSeconds(),
                    time=y+'/'+m+'d'+'/'+h+':'+min+':'+s;
            console.log(time+' app works:'+message);

        }
    })
</script>
<script>
 //1.
 $.log("hello world");
 //2
 $.get_my_date('server start');
 //3.
 $('div>a').css({
        'display':'block'
    }).cssPlugin2({
        'color':'green',
        'fontSize':'40px'
    });
 //4.
    $('#o_a1').cssPlugin2();

 //5.
 $('section>a').myPlugin({
        'color':'green',
        'fontSize':'40px'
    })
</script>

</html>
/**
 * Created by Administrator on 2016/7/23 0023.
 * jquery.myplugin.js
 */
$.extend({
    log:function(message){
        var now = new Date(),
            y = now.getFullYear(),
            m=now.getMonth()+ 1,
            d=now.getDate(),
            h=now.getHours(),
            min = now.getMinutes(),
            s=now.getSeconds(),
            time=y+'/'+m+'/'+d+' '+h+':'+min+":"+s;
        console.log(time+' my app:'+message);

    },
    get_my_date:function(message){
        var now= new Date(),
            y=now.getFullYear(),
            m=now.getMonth()+ 1,
            d=now.getDate(),
            h=now.getHours(),
            min = now.getMinutes(),
            s=now.getSeconds(),
            time=y+'/'+m+'d'+'/'+h+':'+min+':'+s;
        console.log(time+' app works:'+message);

    }
});
$.fn.cssPlugin1=function(){
    this.css('color','red');
    return this.each(function(){
        $(this).append(' '+$(this).attr('href'));
    })
}

$.fn.cssPlugin2=function(options){
    var defaults = {
        'color':'red',
        'fontSize':'12px'
    };
    var settings = $.extend({},defaults,options);
    return this.css({
        'color':settings.color,
        'fontSize':settings.fontSize,
        'display':'block'
    }).each(function(){
        $(this).append(' '+$(this).attr('href'));
    });
}


;(function($, window, document,undefined) {
    //定义Beautifier的构造函数
    var Beautifier = function(ele, opt) {
        this.$element = ele,
            this.defaults = {
                'color': 'red',
                'fontSize': '12px',
                'textDecoration': 'none',
                'display':'block'
            },
            this.options = $.extend({}, this.defaults, opt)
    }
    //定义Beautifier的方法
    Beautifier.prototype = {
        beautify: function() {
            return this.$element.css({
                'color': this.options.color,
                'fontSize': this.options.fontSize,
                'textDecoration': this.options.textDecoration,
                'display':this.options.display
            });
        }
    }
    //在插件中使用Beautifier对象
    $.fn.myPlugin = function(options) {
        //创建Beautifier的实体
        var beautifier = new Beautifier(this, options);
        //调用其方法
        return beautifier.beautify();
    }
})(jQuery, window, document);


参考:
1.http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html
2.http://learn.jquery.com/plugins/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值