jquery后代选择器不同写法的区别? jquery后代选择器有多种写法,如下都能达到目的,哪位高人知道他们之间的核心区别是什么? $("#tab_schedule").find("li"); $("#tab_schedule li"); $("li","#tab_schedule"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 主要是作用上有所区别比如当this 为 #tab_schedule 时 怎么获取 子元素li?$(this).find('li'); 这样就ok$("li","#tab_schedule");这个不是分组选择器吗? 怎么是后代选择器了?可能是楼主这个例子中#tab_schedule 下正好有li而分组 取出来的dom,有li 有#tab_schedule,所以楼主就认为是后代选择器了。 $("#tab_schedule").find("li")、$("li","#tab_schedule")这俩个是完全等价的。 后代选择器的内部实现在文档中没有特别说明。我觉得这三种查找方法在内部实现都是一样的,都是先获取祖宗对象集合,再通过find()方法进行遍历查找,比如:$("#tab_schedule ul li") 等价于 $("#tab_schedule").find("ul").find("li") 楼上(T5500)何以说$("#tab_schedule").find("li")、$("li","#tab_schedule")这俩个是完全等价的,效果一样,原理应该不一样。calmcrime说$("li","#tab_schedule")是分组选择器,怎么理解? 哪儿的概念? jQuery.fn = jQuery.prototype = { constructor: jQuery, init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(""), $(null), or $(undefined) if ( !selector ) { return this; } // Handle $(DOMElement) if ( selector.nodeType ) { this.context = this[0] = selector; this.length = 1; return this; } // The body element only exists once, optimize finding it if ( selector === "body" && !context && document.body ) { this.context = document; this[0] = document.body; this.selector = selector; this.length = 1; return this; } // Handle HTML strings if ( typeof selector === "string" ) { // Are we dealing with HTML string or an ID? 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 = 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] ) { context = context instanceof jQuery ? context[0] : context; 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 = jQuery.buildFragment( [ match[1] ], [ doc ] ); selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes; } 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 } 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 ); }, 上一楼是jQuery对象的初始化函数代码,发现很多种类的选择器最后都是去调用find()方法的。所以可以确定地说,楼主你写的三种方法,最终都是通过find()来实现的,但第一种方法效率是最高的,因为后两种要先经过一系列的if判断后再调用find()方法的,不过这个效率的损失还是比较小的,我觉得可以认为这三种写法是等价的。 - -||$("li","#tab_schedule")确实是获取后代元素和 $('li, span, div') 这个 混淆了。 好样的。 - -||$("li","#tab_schedule") 这个写法我还没接触过呢。 我出血滴。 javascript随机数产生程序。 请问length长度和位置的问题? 如何在JS程序中停一些时间再往下执行? 又有问题了,我想实现三级联动,又该如何修改?问题解决后送上100分,新开贴给分。 关于日期的问题也问的很多,但实现方法却大同小异,写了一个powerDate类 锁定行头和列头的表格组件 关于FileUpload?????? 如何从数据库中取多个字段的值给多唯数组? 请问在JScript中可以使用定时器吗?怎么使用? easyui tree增加业务root节点 求解释JQUERY里的代码 求教uploadify3.1的formdata问题
比如
当this 为 #tab_schedule 时 怎么获取 子元素li?
$(this).find('li');
这样就ok$("li","#tab_schedule");
这个不是分组选择器吗? 怎么是后代选择器了?可能是楼主这个例子中
#tab_schedule 下正好有li
而分组 取出来的dom,有li 有#tab_schedule,所以楼主就认为是后代选择器了。
$("#tab_schedule ul li") 等价于 $("#tab_schedule").find("ul").find("li")
$("#tab_schedule").find("li")、$("li","#tab_schedule")这俩个是完全等价的,效果一样,原理应该不一样。calmcrime说$("li","#tab_schedule")是分组选择器,怎么理解? 哪儿的概念?
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc; // Handle $(""), $(null), or $(undefined)
if ( !selector ) {
return this;
} // Handle $(DOMElement)
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
} // The body element only exists once, optimize finding it
if ( selector === "body" && !context && document.body ) {
this.context = document;
this[0] = document.body;
this.selector = selector;
this.length = 1;
return this;
} // Handle HTML strings
if ( typeof selector === "string" ) {
// Are we dealing with HTML string or an ID?
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 = 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] ) {
context = context instanceof jQuery ? context[0] : context;
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 = jQuery.buildFragment( [ match[1] ], [ doc ] );
selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes;
} 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
} 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 );
},
所以可以确定地说,楼主你写的三种方法,最终都是通过find()来实现的,但第一种方法效率是最高的,因为后两种要先经过一系列的if判断后再调用find()方法的,不过这个效率的损失还是比较小的,我觉得可以认为这三种写法是等价的。
和 $('li, span, div') 这个 混淆了。