自定义的插件,我想写一个方法,类似
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();
这就是jquery的链式写法
$.fn.add = function(el, options){
alert("stewst");
}
这样写就可以了