日期:2014-05-16  浏览次数:20410 次

如何编写一个jquery插件,带传入函数的功能
RT,请给一个范例代码,谢谢~
------解决方案--------------------
jQuery下扩展插件和拓展函数的写法
------解决方案--------------------
	function fn(){
var i=0;
var arg = arguments;
this.style.backgroundColor = 'red';
this.innerText = 'can change ' + (++i) + 'arg1:' + arg[0] + 'arg2' + arg[1];
}

$.fn.myMethod = function(fn,arg1,arg2){
var args = [].slice.call(arguments,1);
this.each(fn,args);
return this;
}
$("div").myMethod(fn,'aa','bb');

------解决方案--------------------
http://www.iteye.com/topic/545971
这个很不错 基本是把英文教程翻译过来了 对插件的教学挺全了
------解决方案--------------------

 (function ($) {
            $.extend($.fn, {           
                myMethod: function (fn, args) {                  
                    //this是全部select 回来的jquery对象集合
                    //用.each loop
                    this.each(function (index, value) {
                        //这里可以写逻辑了                       
                        var value = this.value;  //这里的this是dom 
                        fn(value, this); //调用传进来的方法                      
                    });
                    return this;//把jquery对象集合在返回出去,这样可以连续引用 
                }
            });
        })(jQuery);    
        $(function () {
            $("#inputtext").myMethod(function (value, elem) {
                alert(value);
            }, "args").remove();
        });