jQuery 如何获取具有特定 id 的 <div> 中的所有 <li> 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5939779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get all the <li> elements within a <div> having a specific id?
提问by DenaliHardtail
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"?
看起来很简单,我想我以前做过一两次。用于抓取id为“章节”的所有<li>
元素的jQuery选择器语法是什么<div>
?
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.
我可以获取<li>
元素 with$('li')
和 div ,$('#chapters')
但我需要将选择限制<li>
在该 div 内。
Here is the markup, followed by the jQuery selector. It doesn't work and now I'm at a loss as to why:
这是标记,然后是 jQuery 选择器。它不起作用,现在我不知道为什么:
<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:
JQuery 选择器:
$('#chapters li').css("background-color","red");
回答by Felix Kling
回答by Sumit Rawat
I think you need to use Period(.
) instead of (#
) in the selector. Below is the code:
我认为您需要在选择器中使用 Period( .
) 而不是 ( #
) 。下面是代码:
$('.chapters li').css("background-color","red");