开发者

How do I get all the <li> elements within a <div> having a specific id?

开发者 https://www.devze.com 2023-03-03 19:24 出处:网络
Seems simple enough and I think I\'ve done it before once or twice. What is the jQuery selector syntax for grabbing all the <li> elements inside a <div> with id \"chapters\"?

Seems simple enough and I think I've done it before once or twice. What is the jQuery selector syntax for grabbing all the <li> elements inside a <div> with id "chapters"?

I can get the <li> elements with $('li') and the div with $('#chapters') but I need to limit the selection to <li> within that div.

Here is the markup, followed by the jQuery selector. It doesn't work and now I'm at a loss as to why:

<开发者_开发技巧;li>1 - outside the div</li>

<div id="chapters">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</div>

<li>2 - outside the div</li>

JQuery selector:

$('#chapters li').css("background-color","red");


It is simply

$('#chapters li')

Have a look at the selectors documentation.


I think you need to use Period(.) instead of (#) in the selector. Below is the code:

$('.chapters li').css("background-color","red");
0

精彩评论

暂无评论...
验证码 换一张
取 消