jQuery對(duì)象初始化的傳參方式
jQuery對(duì)象初始化的傳參方式包括:
1.$(DOMElement)
2.$('<h1>...</h1>'), $('#id'), $('.class') 傳入字符串, 這是最常見(jiàn)的形式, 這種傳參數(shù)經(jīng)常也傳入第二個(gè)參數(shù)context指定上下文,其中context參數(shù)可以為$(...), 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)這幾種參數(shù),直接返回this if ( !selector ) { return this; } // 當(dāng)傳參selector為DOM結(jié)點(diǎn)時(shí),將context置為selector if ( selector.nodeType ) { this.context = this[0] = selector; this.length = 1; return this; } // Handle HTML strings // 當(dāng)傳入的selector參數(shù)為字符串時(shí), if ( 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 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 // 當(dāng)selector為function時(shí)相當(dāng)于$(document).ready(selector); } else if ( jQuery.isFunction( selector ) ) { return rootjQuery.ready( selector ); } // 當(dāng)selector參數(shù)為{selector:'#id', context:document}之類(lèi)時(shí),重置屬性selector和context if ( selector.selector !== undefined ) { this.selector = selector.selector; this.context = selector.context; } return jQuery.makeArray( selector, this ); } };
以上就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JQuery使用屬性addClass、removeClass和toggleClass實(shí)現(xiàn)增加和刪除類(lèi)操作示例
這篇文章主要介紹了JQuery使用屬性addClass、removeClass和toggleClass實(shí)現(xiàn)增加和刪除類(lèi)操作,涉及jquery事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)使用技巧,需要的朋友可以參考下2019-11-11詳解layui中的樹(shù)形關(guān)于取值傳值問(wèn)題
本篇文章主要介紹了詳解layui中的樹(shù)形關(guān)于取值傳值問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01jQuery實(shí)現(xiàn)移動(dòng)端手機(jī)商城購(gòu)物車(chē)功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)移動(dòng)端手機(jī)商城購(gòu)物車(chē)功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09jquery ui dialog實(shí)現(xiàn)彈窗特效的思路及代碼
這篇文章介紹了jquery ui dialog實(shí)現(xiàn)彈窗特效的思路及代碼,有需要的朋友可以參考一下2013-08-08兩個(gè)select之間option的互相添加操作(jquery實(shí)現(xiàn))
兩個(gè)select,將其中一個(gè)select選中的選項(xiàng)添加到另一個(gè)select中,或者點(diǎn)擊全部添加按鈕將所有的option都添加過(guò)去.2009-11-11jquery實(shí)現(xiàn)的一個(gè)文章自定義分段顯示功能
基于jquery實(shí)現(xiàn)的文章自定義分段顯示,如果文章內(nèi)容太多的話轉(zhuǎn)換有點(diǎn)慢,大家若喜歡的話,可以參考下2014-05-05jquery獲取所有選中的checkbox實(shí)現(xiàn)代碼
下面小編就為大家?guī)?lái)一篇jquery獲取所有選中的checkbox實(shí)現(xiàn)代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05