您的位置: 网站首页> IT爱问> 当前文章

前端面试题演示一下jQuery扩展插件

老董2021-09-25151围观,120赞

  前端面试题演示一下jQuery扩展插件。

  (1)考察目标

  1)考察是否自己编写过拓展插件

  2)是否知道为jQuery扩展插件的方法

  (2)题目分析

  1)通过$.extend()来扩展jQuery

  2)通过$.fn 向jQuery添加新的方法

  代码

// 1. 通过$.extend()来扩展jQuery
        // 语法:$.extend({})
        // 缺点:这种方式无法利用jQuery强大的选择器带来的便利,

        $.extend({            log: function(msg) {                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 + '--' + msg);
            }
        })
        $.log('initializing'); //调用

        // 2. 通过$.fn 向jQuery添加新的方法
        /*
        语法
        $.fn.pluginName = function() {
        //your code goes here
        }
        */
        // 常用的方式
        $.fn.changeColor = function() {            //在这里面,this指的是用jQuery选中的元素
            this.css('color', 'red');
        }

        $(function() {
            $('a').changeColor();
        })
    

  (3)应用场景

  通过$.fn 向jQuery添加新的方法 是开发中常用的写法,因为这种写法可以利用jQuery强大的选择器。

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    前端面试题演示一下jQuery扩展插件文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200