jQueryオブジェクト初期化のパラメータ伝達方式
3831 ワード
jQueryオブジェクトの初期化のパラメータ伝達方法は、次のとおりです.
1.$(DOMElement) 2.$('...'),$('#id'),$('.class')は文字列を入力します.これは最も一般的な形式です.このパラメータは、contextパラメータが$(...)であることもよくあります.DOMElement 3.$(function() {}); <===> $(document).ready(function() { }); 4.$({selector : '.class', context : context}) <===> $('.class', context)
以上が本文のすべての内容で、みんなが好きになることを望みます.
1.$(DOMElement) 2.$('...'),$('#id'),$('.class')は文字列を入力します.これは最も一般的な形式です.このパラメータは、contextパラメータが$(...)であることもよくあります.DOMElement 3.$(function() {}); <===> $(document).ready(function() { }); 4.$({selector : '.class', context : context}) <===> $('.class', context)
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc;
// $(""), $(null), $(undefined), $(false) , this
if ( !selector ) {
return this;
}
// selector DOM , context selector
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
// Handle HTML strings
// selector ,
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 {
match = rquickExpr.exec( selector );
}
// Match html or make sure no context is specified for #id
if ( match && (match[1] || !context) ) {
// 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)
// Shortcut for document ready
// selector function $(document).ready(selector);
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
// selector {selector:'#id', context:document} , selector context
if ( selector.selector !== undefined ) {
this.selector = selector.selector;
this.context = selector.context;
}
return jQuery.makeArray( selector, this );
}
};
以上が本文のすべての内容で、みんなが好きになることを望みます.