pingpang

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            21 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks
          jQuery對象初始化的傳參方式包括:
          1.$(DOMElement)
          2.$('<h1>...</h1>'), $('#id'), $('.class') 傳入字符串, 這是最常見的形式, 這種傳參數(shù)經(jīng)常也傳入第二個參數(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é)點時,將context置為selector
                  if ( selector.nodeType ) {
                      this.context = this[0] = selector;
                      this.length = 1;
                      return this;
                  }

                  // Handle HTML strings
                  // 當(dāng)傳入的selector參數(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時相當(dāng)于$(document).ready(selector);
                  } else if ( jQuery.isFunction( selector ) ) {
                      return rootjQuery.ready( selector );
                  }

                  // 當(dāng)selector參數(shù)為{selector:'#id', context:document}之類時,重置屬性selector和context
                  if ( selector.selector !== undefined ) {
                      this.selector = selector.selector;
                      this.context = selector.context;
                  }

                  return jQuery.makeArray( selector, this );
              }
          }; 
          posted on 2012-08-16 22:39 往事隨風(fēng) 閱讀(1808) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 淅川县| 桑植县| 嘉荫县| 台东市| 麻江县| 武安市| 普兰店市| 房产| 巴马| 古蔺县| 离岛区| 呼玛县| 镇沅| 鸡西市| 汶上县| 临洮县| 阳城县| 闵行区| 江都市| 连平县| 莫力| 英德市| 临沧市| 岚皋县| 江北区| 西华县| 习水县| 监利县| 武义县| 阿合奇县| 临沂市| 修文县| 杭州市| 洱源县| 垫江县| 孟村| 三门峡市| 仲巴县| 鸡泽县| 廊坊市| 普宁市|