Some options for jQuery selector?

I just looked at the jQueryUI button plugin and noticed this

$("button, input:submit, a", ".demo").button(); 

I have never seen anything like it. Does this look like multiple selections in a single jQuery selector?

+93
jquery
Apr 20 2018-10-10T00:
source share
1 answer

The second argument ( ".demo" in your example) is the context, basically your selector is limited to match only the descendants of the specific context:

 $(expr, context) 

This is simply equivalent to using the find method:

 $(context).find(expr) 

Take a look at the jQuery function documentation:

Selector context

By default, selectors perform their search in the DOM, starting at the root of the document. However, an alternative context can be set to search using the extra second parameter in the $() function. For example, if inside the callback function we want to search for an element, we can limit this search:

 $('div.foo').click(function() { $('span', this).addClass('bar'); // it will find span elements that are // descendants of the clicked element (this) }); 

Also note that the selector you send "button, input:submit, a" is called the Multiple Selector , and there you can specify any number of selectors to combine into a single result, just separating them with a comma.

+146
Apr 20 2018-10-10T00:
source share



All Articles