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]
解决方案 »
- form表单提交问题
- 求判断浏览器类型的javascript代码(如IE,360,TT,火狐,搜狗,世界之窗)
- js小问题
- 请问document.XXX.address.value中XXX如果为变量,应该如何写这个代码?
- 菜鸟求助!!!急急急
- 关于bt1.btchina.net网页加密的问题,老鸟请进!Up没分!
- 请问,在JAVASCRIPT中,有什么函数可以把一个数字串转换成数字??????
- 创建新的option的问题
- 请教一下有没有类似于extjs那样的,网站后台管理框架?
- JS在360浏览器的兼容模式下无法触发
- 要想删除列表里的最后一个LI该怎么做啊?
- 关于innerHTML 写出file的问题
jquery选择器根据当前选择的文档返回一个对象或是一个数组
如:$("a") 如果有多个链接就是一个数组了,而一个链接则是一个对象
具体看jquery的返回值,返回值有each。each执行一次就返回个对象,多次就是数组了
如果get方法中的this[0],相当于array[0]
因为当前对象是一个数组,this指向这个数组。this[0]数组第1个
$("a[href$='headerHome']")[0]和$("a[href$='headerHome']").get(0)得到的结果一样
可否贴出关键代码
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 了 不知道这个时候数组是怎么产生的
this 是指向jQuery.prototype
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;
}