How do I select multiple elements and a class in J

2019-03-04 02:58发布

问题:

So I was asked a simple question on Treehouse, but I'm confused about how to order the code.

I was asked to use jQuery to select all list items in an unordered list with the class of 'nav'. I'm not sure how to order my code (I'm literally brand new to JQuery). Here's my incorrect answer.

$("li ul .nav");

回答1:

The following selects all list items in a list that has the .nav class (<ul class="nav">...</ul>):

$("ul.nav li");


回答2:

Try this:

$('ul.nav li');

This selects every child li in ul with the class 'nav'.



回答3:

Ancestor elements need to be first, as jQuery looks "down" from the <html> element, and to select ul's with a class .nav you need to do ul.nav.

$('ul.nav li');