jquery后代选择器有多种写法,如下都能达到目的,哪位高人知道他们之间的核心区别是什么?
$("#tab_schedule").find("li");
$("#tab_schedule li"); $("li","#tab_schedule");
$("#tab_schedule").find("li");
$("#tab_schedule li"); $("li","#tab_schedule");
解决方案 »
- document.body.scrollTop和document.documentElement.scrollTop返回值都是0,在线跪求
- 谁给我提供一段简单的js带左右控制的点击切换代码?
- 菜鸟的小问题
- 删除不了cookie!!头疼..急啊..
- 关于对联广告不能滚动的问题
- 关于innertext的问题
- 用window.setInterval设置时间间隔执行指定的代码后如何使其停止?
- ★★★1.怎么用javascript屏蔽IE的菜单?2.在javascript中怎么调用window API
- 一个Table表格问题
- 本人不懂JS,求JS大神帮忙解决一个问题
- 求解释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') 这个 混淆了。