如何编写一个jquery插件,带传入函数的功能 jQuery下扩展插件和拓展函数的写法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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'); 明确一下,应该这样写function fn(v1,v2){ var that = this; } $.fn.myMethod = function(fn,v1,v2){ return this.each(function(k,v){ //fn(v1,v2) //this is window; $.proxy(fn,this)(v1,v2); //this is the element }) } $("div").myMethod(fn); 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(); }); JS确定删除问题 求修改一JS 代码 当我鼠标点击那个复选框的时候为什么无法选中呢? 一个隐藏的问题,高手们帮帮忙吧 求高效脚本!取任意两个日期中间的所有日期 在IFrame中使用<a href="javascript:XXXFunction();" target="_parent" style="text-decoration></a> 如何用脚本隐藏select中的一个option? 请问怎么给file input 赋值? 恩,如何记住上一个 obj…… 怎么出声呀? Jquery动态添加的li实现键盘控制li JS可以监视字体文件下载完成吗?
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');
function fn(v1,v2){
var that = this;
}
$.fn.myMethod = function(fn,v1,v2){
return this.each(function(k,v){
//fn(v1,v2) //this is window;
$.proxy(fn,this)(v1,v2); //this is the element
})
}
$("div").myMethod(fn);
这个很不错 基本是把英文教程翻译过来了 对插件的教学挺全了
(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();
});