.has( selector )Returns: jQuery
Description: Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.
-
version added: 1.4.has( selector )
-
selector
A string containing a selector expression to match elements against.
-
-
version added: 1.4.has( contained )
-
contained
A DOM element to match elements against.
-
Given a jQuery object that represents a set of DOM elements, the .has() method constructs a new jQuery object from a subset of the matching elements. The supplied selector is tested against the descendants of the matching elements; the element will be included in the result if any of its descendant elements matches the selector.
Consider a page with a nested list as follows:
|
1 2 3 4 5 6 7 8 9 10 11 |
|
We can apply this method to the set of list items as follows:
|
1 |
|
The result of this call is a red background for item 2, as it is the only <li> that has a <ul> among its descendants.
Example:
Check if an element is inside another.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|