what does this mean: “jQuery('> li', this)”

删除回忆录丶 提交于 2019-12-30 09:44:13

问题


I'm trying to figure out how this jQuery plugin works: http://codeasily.com/jquery/multi-column-list-with-jquery

In the plugin there is this line at the beginning:

 if(jQuery('> li', this)) {

I know what

ul > li

means: it means select all li whose direct parent is a ul. But what does '> li' mean? I ran:

$('> li')

but it returns

[]

even though I have plenty of nested unordered list HTML on the page.


回答1:


Don't use it. The docs advise that you shouldn't use at as it will be soon deprecated.

From http://api.jquery.com/child-selector/

Note: The $("> elem", context) selector will be deprecated in a future release. Its usage is thus discouraged in lieu of using alternative selectors.


But to answer, it effectively uses the element(s) from which the selector is called as the left hand operand of the child-selector.




回答2:


The 2nd parameter to the jQuery function is the context.

jQuery('> li', this)

Is the same as:

jQuery(this).find('> li')



回答3:


It's the same as $(this).children('li'). It's basically saying "use this as the context for the selector (> li)".




回答4:


its jsut like ul > li except that in this case you are replacing the ul part with the current context which is this. So whatever this is in the scope of that call that is the element you are resolving the > li to.

So for example:

var ele = $('ul#someId');
var list = $('> li', ele);


var list2 = $('ul#someId > li');

// list is the same as list2



回答5:


It will look for li element in the immediate children within this element where this can be a jQuery object or DOM element.




回答6:


The selector is looking for any immediate <li> children in the context of the this DOM element



来源:https://stackoverflow.com/questions/8899415/what-does-this-mean-jquery-li-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!