首页 新闻 会员 周边 捐助

Jquery插件中的方法

0
悬赏园豆:100 [已关闭问题] 关闭于 2020-02-14 16:02

自定义的插件,我想写一个方法,类似  

var _data = $('#kt_tree_3').jstree(true).get_selected();

中的get_selected(),

插件的代码如下 :

(function ($) {

    $.fn.hilight = function (options) {

        // Extend our default options with those provided.
        // Note that the first argument to extend is an empty
        // object – this is to keep from overriding our "defaults" object.
        var opts = $.extend({}, $.fn.hilight.defaults, options);
       
        // Our plugin implementation code goes here.
        return this.each(function () {

            var elem = $(this);

            var markup = elem.html();

            // Call our format function.
            markup = $.fn.hilight.format(markup);

            elem.html(markup);
            elem.css("background-color", opts.background);
            elem.css("color", opts.foreground);

        });   
    };

    // Plugin defaults – added as a property on our plugin function.
    $.fn.hilight.defaults = {
        foreground: "red",
        background: "yellow"
    };
    $.fn.hilight.format = function (txt) {
        return "<strong>" + txt + "</strong>";
    };

    this.add =  function (el, options) {
            alert("stewst");
        
    };

})(jQuery);

我想它的Add方法生效,如何写呢?

  $("p").hilight({
                foreground: "green"
            }).add();
happydaily的主页 happydaily | 菜鸟二级 | 园豆:260
提问于:2020-02-14 15:53
< >
分享
所有回答(1)
0

这就是jquery的链式写法
$.fn.add = function(el, options){
alert("stewst");
}
这样写就可以了

lsvs | 园豆:182 (初学一级) | 2020-05-12 11:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册