Heim  >  Artikel  >  Web-Frontend  >  JQuery1.6 使用方法三_jquery

JQuery1.6 使用方法三_jquery

WBOY
WBOYOriginal
2016-05-16 17:59:021077Durchsuche

a={id:1,get:function(){alert(this.id)}}
  $("#test").click(a.get)//这时候所指的id就不是1,而是test
  如果想要得到的是1,那就必须改变上下文环境$.
  $.proxy()的作用就是改变当前上下文环境。
  $("#test").click($.proxy(a,"get"))
  $("#test").click($.proxy(a.get,a))
  proxy: function( fn, context ) {//改变函数上下文环境,this指向设置的对象
    这里的参数可以有两种方式:函数fn fn的方法context
                 函数的方法fn[context] 函数fn
if ( typeof context === "string" ) {//如果content是字符串,那么会认为是fn的一个方法 fn[ context ],并在接下来判断是否是函数
var tmp = fn[ context ];
context = fn;
fn = tmp;
}
if ( !jQuery.isFunction( fn ) ) {//不是函数立刻返回undefined
return undefined;
}
var args = slice.call( arguments, 2 ),//把fn,content后面的参数作为要执行的函数参数
proxy = function() {
return fn.apply( context, args.concat( slice.call( arguments ) ) );//返回设置了指定上下文环境的可执行函数,fn为可执行行数,把this指向content,args.concat( slice.call( arguments ) )作为参数,
};
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;//一个全局的计数器,用于标识该函数可以用来删除
return proxy;
},
//access这个方法主要是在jquery内部使用,用于attr,prop,css;主要设置DOM组(jquery对象)单个或者多个属性、样式的值,获取DOM组(jquery对象)某个属性、样式的值
//elems操作的对象组,key属性,value属性值,exec默认true,fn用来操作的函数,pass主要在attr的时候设置val,css,html,text,data,width,height,offset的时候应该为true.
access: function( elems, key, value, exec, fn, pass ) {
var length = elems.length;
//设置多个属性
if ( typeof key === "object" ) {//如果属性值是对象,将循环设置dom组(jquery对象)的属性和对应的值
for ( var k in key ) {
jQuery.access( elems, k, key[k], exec, fn, value );
}
return elems;//返回结果
}
// 设置单个属性
if ( value !== undefined ) {//如果value存在,设置某个属性,可以是一组对象或者单个;如果不存在,即获取doms[0](jquery对象)某个属性值,只能返回带一个对象,或者由于对象不存在直接返回undefiend;
exec = !pass && exec && jQuery.isFunction(value);//目前jquery默认pass为空null,exec为true,value是否为函数
for ( var i = 0; i fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );//如果exec为true,,传入操作对象的序列和操作属性值作为参数,否则,设置属性key的值为value
}
return elems;
}
// 获取属性
return length ? fn( elems[0], key ) : undefined;
},
now: function() {//返回当前时间
return (new Date()).getTime();
},

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn