The has method lets you reduce the set of selected elements to those that have a
ID: 3694239 • Letter: T
Question
The has method lets you
reduce the set of selected elements to those that have a sibling that matches the specified selector
reduce the set of selected elements to those that have a child that matches the specified selector
reduce the set of selected elements to those that match the specified selector
reduce the set of selected elements to those that have a descendant that matches the specified selector
a.reduce the set of selected elements to those that have a sibling that matches the specified selector
b.reduce the set of selected elements to those that have a child that matches the specified selector
c.reduce the set of selected elements to those that match the specified selector
d.reduce the set of selected elements to those that have a descendant that matches the specified selector
Explanation / Answer
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
<ul>
<li>list item 1</li>
<li>list item 2
<ul>
<li>list item 2-a</li>
<li>list item 2-b</li>
</ul>
</li>
<li>list item 3</li>
<li>list item 4</li>
</ul>
We can apply this method to the set of list items as follows:
$( "li" ).has( "ul" ).css( "background-color", "red" );
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.
Answer
(d) reduce the set of selected elements to those that have a descendant that matches the specified selector
1
2
3
4
5
6
7
8
9
10
11
<ul>
<li>list item 1</li>
<li>list item 2
<ul>
<li>list item 2-a</li>
<li>list item 2-b</li>
</ul>
</li>
<li>list item 3</li>
<li>list item 4</li>
</ul>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.