jquery源码分析(2) jQuery对象的一些方法和属性 1
来源:互联网 发布:盛势网络剧微博 编辑:程序博客网 时间:2024/04/28 21:12
本文将分析(96 , 283) 给JQ对象,添加的方法和属性
首先简单列出JQ对象方法和属性大纲:
jQuery.fn = jQuery.protogype = {
jquery : 版本
constructor : 修正指向问题
init() : 初始化和参数管理
selector : 存储选择字符串
length : this对象长度
toArray() : 转数组
get() : 转原生集合
pushStack() : JQ对象的入栈
each() : 遍历集合
ready() : DOM加载的接口
slice() : 集合的截取
first() : 集合的第一项
last() : 集合的最后一项
eq() : 集合的指定项
map() : 返回新集合
end() : 返回集合前一个状态
push() : 内部使用
sort() : 内部使用
splice() : 内部使用
}
所有的源代码如下:
jQuery.fn = jQuery.prototype = {// The current version of jQuery being usedjquery: core_version,constructor: jQuery,init: function( selector, context, rootjQuery ) {var match, elem;// HANDLE: $(""), $(null), $(undefined), $(false)if ( !selector ) {return this;}// Handle HTML stringsif ( typeof selector === "string" ) {if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {// Assume that strings that start and end with <> are HTML and skip the regex checkmatch = [ null, selector, null ];} else {match = rquickExpr.exec( selector );}// Match html or make sure no context is specified for #idif ( match && (match[1] || !context) ) {// HANDLE: $(html) -> $(array)if ( match[1] ) {context = context instanceof jQuery ? context[0] : context;// scripts is true for back-compatjQuery.merge( this, jQuery.parseHTML(match[1],context && context.nodeType ? context.ownerDocument || context : document,true) );// HANDLE: $(html, props)if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {for ( match in context ) {// Properties of context are called as methods if possibleif ( jQuery.isFunction( this[ match ] ) ) {this[ match ]( context[ match ] );// ...and otherwise set as attributes} else {this.attr( match, context[ match ] );}}}return this;// HANDLE: $(#id)} else {elem = document.getElementById( match[2] );// Check parentNode to catch when Blackberry 4.6 returns// nodes that are no longer in the document #6963if ( elem && elem.parentNode ) {// Inject the element directly into the jQuery objectthis.length = 1;this[0] = elem;}this.context = document;this.selector = selector;return this;}// HANDLE: $(expr, $(...))} else if ( !context || context.jquery ) {return ( context || rootjQuery ).find( selector );// HANDLE: $(expr, context)// (which is just equivalent to: $(context).find(expr)} else {return this.constructor( context ).find( selector );}// HANDLE: $(DOMElement)} else if ( selector.nodeType ) {this.context = this[0] = selector;this.length = 1;return this;// HANDLE: $(function)// Shortcut for document ready} else if ( jQuery.isFunction( selector ) ) {return rootjQuery.ready( selector );}if ( selector.selector !== undefined ) {this.selector = selector.selector;this.context = selector.context;}return jQuery.makeArray( selector, this );},// Start with an empty selectorselector: "",// The default length of a jQuery object is 0length: 0,toArray: function() {return core_slice.call( this );},// Get the Nth element in the matched element set OR// Get the whole matched element set as a clean arrayget: function( num ) {return num == null ?// Return a 'clean' arraythis.toArray() :// Return just the object( num < 0 ? this[ this.length + num ] : this[ num ] );},// Take an array of elements and push it onto the stack// (returning the new matched element set)pushStack: function( elems ) {// Build a new jQuery matched element setvar ret = jQuery.merge( this.constructor(), elems );// Add the old object onto the stack (as a reference)ret.prevObject = this;ret.context = this.context;// Return the newly-formed element setreturn ret;},// Execute a callback for every element in the matched set.// (You can seed the arguments with an array of args, but this is// only used internally.)each: function( callback, args ) {return jQuery.each( this, callback, args );},ready: function( fn ) {// Add the callbackjQuery.ready.promise().done( fn );return this;},slice: function() {return this.pushStack( core_slice.apply( this, arguments ) );},first: function() {return this.eq( 0 );},last: function() {return this.eq( -1 );},eq: function( i ) {var len = this.length,j = +i + ( i < 0 ? len : 0 );return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] );},map: function( callback ) {return this.pushStack( jQuery.map(this, function( elem, i ) {return callback.call( elem, i, elem );}));},end: function() {return this.prevObject || this.constructor(null);},// For internal use only.// Behaves like an Array's method, not like a jQuery method.push: core_push,sort: [].sort,splice: [].splice};// Give the init function the jQuery prototype for later instantiationjQuery.fn.init.prototype = jQuery.fn;
0 0
- jquery源码分析(2) jQuery对象的一些方法和属性 1
- jquery源码分析(2) jQuery对象的一些方法和属性 2
- jquery源码分析(2) jQuery对象的一些方法和属性 3
- jQuery源码学习之三 (jQUery对象的实例属性和方法)
- jQuery源码学习之四 (jQUery对象的实例属性和方法)
- jQuery源码学习 item2-jQuery对象及其属性和方法
- jquery对象和DOM对象调用一些方法及属性的区别(收集中)
- jQuery 事件对象的属性和方法
- jquery的event对象属性和方法
- jQuery Event对象的属性和方法
- jQuery jqXHR对象的属性和方法
- jQuery jqXHR对象的属性和方法
- jQuery jqXHR对象的属性和方法
- jQuery事件对象的属性和方法
- jQuery事件对象的属性和方法
- jQuery事件对象的属性和方法
- jQuery源码研究分析学习笔记-jQuery原型属性和方法(九)
- jQuery源码研究分析学习笔记-静态方法和属性(10)
- Python3.4字符串包含 & 集合
- 黑马程序员----交通灯管理系统
- 杭电 1276 士兵队列训练问题
- 设计模式之备忘录
- 关于蓝牙接收数据的解决方案
- jquery源码分析(2) jQuery对象的一些方法和属性 1
- 杭电2050折线分割平面
- Algorithm学习笔记 --- 百钱买百鸡问题(枚举练习)
- 点球大战 2539
- 挺有意思的文章。。。
- PostgreSQL 和Greenplum 区别
- 40-Service学习第一发Service的生命周期
- AIX下VIM的安装
- UISlider和UIScreenEdgePanGestureRecognizer和简单运用,包括滑动时颜色变化,滑动按钮以及Slider按钮颜色设置