var a = $("a[href$='headerHome']")//取出href  属性以headerHome结尾的链接,此对象是经过 jquery 包装的//下面二个是取原始对象
var a = $("a[href$='headerHome']")[0]  var a = $("a[href$='headerHome']").get(0)我在查询 了 jquery   源码,get方法中有 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
想问得是 jquery对象是个数组吗(相关源代码在哪个方法里),为什么 get 方法里可以用this[0] ,也可以这样$("a[href$='headerHome']")[0]

解决方案 »

  1.   

    另外,如果有个元素 id  为test #('#test')取到的是jquery   包装过的对象,要得到原始对象除了我发的贴子上面二种方法,还有没有更简单的方法取得原始始对象
      

  2.   


    jquery选择器根据当前选择的文档返回一个对象或是一个数组
    如:$("a") 如果有多个链接就是一个数组了,而一个链接则是一个对象
    具体看jquery的返回值,返回值有each。each执行一次就返回个对象,多次就是数组了
    如果get方法中的this[0],相当于array[0]
    因为当前对象是一个数组,this指向这个数组。this[0]数组第1个
    $("a[href$='headerHome']")[0]和$("a[href$='headerHome']").get(0)得到的结果一样
      

  3.   


      可否贴出关键代码
    if ( typeof selector === "string" ) {
    // Are we dealing with HTML string or an ID?
    match = quickExpr.exec( selector ); // Verify a match, and that no context was specified for #id
    if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array)
    if ( match[1] ) {
    doc = (context ? context.ownerDocument || context : document); // If a single string is passed in and it's a single tag
    // just do a createElement and skip the rest
    ret = rsingleTag.exec( selector ); if ( ret ) {
    if ( jQuery.isPlainObject( context ) ) {
    selector = [ document.createElement( ret[1] ) ];
    jQuery.fn.attr.call( selector, context, true ); } else {
    selector = [ doc.createElement( ret[1] ) ];
    } } else {
    ret = buildFragment( [ match[1] ], [ doc ] );
    selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
    }

    return jQuery.merge( this, selector );

    // HANDLE: $("#id")
    } else {
    elem = document.getElementById( match[2] ); if ( elem ) {
    // 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: $("TAG")
    } else if ( !context && /^\w+$/.test( selector ) ) {
    this.selector = selector;
    this.context = document;
    selector = document.getElementsByTagName( selector );
    return jQuery.merge( this, selector ); // 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 jQuery( context ).find( selector );
    } // 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 );
    },
    最后一行,是有返回数组( makeArray)但有些情况下就直接  return this 了 不知道这个时候数组是怎么产生的
      

  4.   

    $..得到的是数组,有length属性,你可以调试看看吧!是一个个的Dom数组
      

  5.   

    this   就是指向jquery 对象,还没找到最后为什么变成数组的源代码
      

  6.   

    说错了
    this  是指向jQuery.prototype
      

  7.   


    JQuery对象是一个类数组对象,不是真正的数组。这时jQuery定义的入口
    jQuery = window.jQuery = window.$ = function( selector, context ) {
      // The jQuery object is actually just the init constructor 'enhanced'
      // jQuery对象实际上只是初始化构造器的加强版而已。   //返回new对象 这里使得jQuery对象可以直接构建,而不需要使用new方式。
       return new jQuery.fn.init( selector, context );
    },
    而: jQuery.fn.init.prototype = jQuery.fn;
    因此上述new实际执行的是jQuery.fn.init(...)函数
    在该函数中,有一段很经典的代码
    if ( selector.nodeType ) {
            // 如果是DOM元素
     
            // 看起来是一个数组的样子,实质是加入属性值0,它的值为
            // 对应的DOM元素。看起来就象数组的0元素
    this[0] = selector;
            // 加上长度属性,更象数组了
    this.length = 1;
    this.context = selector;
    return this;
    }