jquery 소스 - jquery ()

자세히 보기
원본 95~289 줄, jquery.fn 핵심 함수(utils)

jQuery.fn = jQuery.prototype = {
	constructor: jQuery,
	init: function( selector, context, rootjQuery ) {},//     

	// Start with an empty selector
	selector: "",

	// The current version of jQuery being used
	jquery: "1.8.2",//    

	// The default length of a jQuery object is 0
	length: 0,//  jquery        

	// The number of elements contained in the matched element set
	size: function() {},//       

	toArray: function() {},//    

	// Get the Nth element in the matched element set OR
	// Get the whole matched element set as a clean array
	get: function( num ) {},//  jquery     num+1   

	// Take an array of elements and push it onto the stack
	// (returning the new matched element set)
	pushStack: function( elems, name, selector ) {},// DOM    elems  jquery  

	// 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 ) {},// jQuery.each

	ready: function( fn ) {},// jQuery.ready.promise()

	eq: function( i ) {},//   i+1   

	first: function() {},//       

	last: function() {},//        

	slice: function() {},

	map: function( callback ) {},

	end: function() {},

	// 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 instantiation
jQuery.fn.init.prototype = jQuery.fn;


init 방법 init 방법은 실제적으로 juqery의 주 함수 $() 또는 jQuery() $() 인자:selector: 선택기(문자열,dom 요소,수 그룹,함수,jQuery 대상,null,undefined,false) content: 찾을 DOM 요소 집합, 문서 또는 jQuery 대상입니다.rootjQuery: 현재 문서 역할 영역의 루트 노드

init: function( selector, context, rootjQuery ) {
		var match, elem, ret, doc;

		// Handle $(""), $(null), $(undefined), $(false)
		if ( !selector ) {
			return this;
		}

		// Handle $(DOMElement)     ,  DOM   $(body)
		if ( selector.nodeType ) {
			this.context = this[0] = selector;
			this.length = 1;
			return this;
		}

		// Handle HTML strings //html   
		if ( typeof selector === "string" ) {//''        $('')
			if ( selector.charAt(0) === "" && selector.length >= 3 ) {
				// Assume that strings that start and end with <> are HTML and skip the regex check
				match = [ null, selector, null ];

			} else {// html      #id      
                                
				match = rquickExpr.exec( selector );
			}

			// Match html or make sure no context is specified for #id
			if ( match && (match[1] || !context) ) {
                                //  $("

Hello

") // HANDLE: $(html) -> $(array) if ( match[1] ) { context = context instanceof jQuery ? context[0] : context; doc = ( context && context.nodeType ? context.ownerDocument || context : document ); // scripts is true for back-compat selector = jQuery.parseHTML( match[1], doc, true ); if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) { this.attr.call( selector, context, true ); } return jQuery.merge( this, selector ); // 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 #6963 if ( elem && elem.parentNode ) { // Handle the case where IE and Opera return items // by name instead of ID if ( elem.id !== match[2] ) { return rootjQuery.find( selector ); } // Otherwise, we inject the element directly into the jQuery object this.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: $(function) // $(function(){ //Document is ready //}); // 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 ); }

1.jQuery() 또는 $()

//Handle $(""), $(null), $(undefined), $(false), $()
if ( !selector ) {
	return this;//     
}

2. jQuery(element) 또는 $(element)

// Handle $(DOMElement)     ,  DOM   $(body)
if ( selector.nodeType ) {
	this.context = this[0] = selector;//      context    DOM  
	this.length = 1;
	return this;
}

3. jQuery(') 또는 $(')

// Handle $('',document.forms[0]) 
//     "ddd시나닷컴asd"   ,  match[1]   "시나닷컴"
if ( match[1] ) {
    context = context instanceof jQuery ? context[0] : context;//context    jQuery  
    //     ,     DOM   ,     ,   document  
    doc = ( context && context.nodeType ? context.ownerDocument || context : document );

    // scripts is true for back-compat
    selector = jQuery.parseHTML( match[1], doc, true );
    if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
        this.attr.call( selector, context, true );
    }
    return jQuery.merge( this, selector );
}else{
 //
}

4.jQuery(#id) 또는 $(#id)

{
    //match[2]   #id   ,  match[2]      id     , “#name” match[2]  “name”
    elem = document.getElementById( match[2] );

    // Check parentNode to catch when Blackberry 4.6 returns
    // nodes that are no longer in the document #6963
    if ( elem && elem.parentNode ) {
        // Handle the case where IE and Opera return items
        // by name instead of ID
        //  IE Opera      getElementById        name        ,           name      
        if ( elem.id !== match[2] ) {
            return rootjQuery.find( selector );
        }

        // Otherwise, we inject the element directly into the jQuery object
        this.length = 1;
        this[0] = elem;
    }

    this.context = document;
    this.selector = selector;
    return this;
}

5.jQuery(callback) 또는 $(callback)

   return rootjQuery.ready( selector );//      function 

6.jQuery(expr, $(...))또는 $(expr, $(...),예를 들어 $("div>p", $(...), $("input:radio",document.forms[0]),$("div.foo",document.forms[0])

if ( !context || context.jquery ) {
   return ( context || rootjQuery ).find( selector  );//
}

7. 상기 모든 상황 외의 기타 상황

    // HANDLE: $(expr, context)
    return this.constructor( context ).find( selector );

좋은 웹페이지 즐겨찾기