Javascript 如何在 DIV 中选择带有类的项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6933763/
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 can I select item with class within a DIV?
提问by Idan Shechter
I have the following HTML:
我有以下 HTML:
<div id="mydiv">
<div class="myclass"></div>
</div>
I want to be able to use a selector that selects the inside div
, but specific for the mydiv
container. How can I achieve this with jQuery?
我希望能够使用一个选择器来选择 inside div
,但特定于mydiv
容器。我怎样才能用 jQuery 实现这一点?
回答by David says reinstate Monica
Try:
尝试:
$('#mydiv').find('.myclass');
Or:
或者:
$('.myclass','#mydiv');
Or:
或者:
$('#mydiv .myclass');
References:
参考:
Good to learn from the find()
documentation:
很高兴从find()
文档中学习:
The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.
.find() 和 .children() 方法是相似的,除了后者只沿着 DOM 树向下移动一层。
回答by ShankarSangoli
Try this
尝试这个
$("#mydiv div.myclass")
回答by Leandro Galluppi
You'll do it the same way you would apply a css selector. For instanse you can do
您将采用与应用 css 选择器相同的方式来执行此操作。例如,你可以做
$("#mydiv > .myclass")
or
或者
$("#mydiv .myclass")
The last one will match every myclass inside myDiv, including myclass inside myclass.
最后一个将匹配 myDiv 中的每个 myclass,包括 myclass 中的 myclass。
回答by e11438
If you want to select every element that has class attribute "myclass" use
如果要选择具有类属性“myclass”的每个元素,请使用
$('#mydiv .myclass');
If you want to select only div elements that has class attribute "myclass" use
如果您只想选择具有类属性“myclass”的 div 元素,请使用
$("div#mydiv div.myclass");
find more about jquery selectors refer these articles
查找有关 jquery 选择器的更多信息,请参阅这些文章
回答by user3749775
try this instead $(".video-divs.focused")
. This works if you are looking for video-divs that are focused.
试试这个$(".video-divs.focused")
。如果您正在寻找专注的视频 div,这将起作用。