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

2019-02-17 18:45发布

问题:

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