简体   繁体   English

使用jQuery在另一个元素中按类名获取元素

[英]Get elements by class name inside another element with jQuery

How can I get an element by id, and then inside this element get all elements by class name using jQuery? 如何通过id获取元素,然后在这个元素中使用jQuery按类名获取所有元素? It's pretty easy to do this using the standard JS functions getElementById() and getElementsByClassName(), but unfortunately IE 7-8 do not support the latter. 使用标准JS函数getElementById()和getElementsByClassName()很容易做到这一点,但遗憾的是IE 7-8不支持后者。

You have a few options: 你有几个选择:

The first, using a css selector: 第一个,使用css选择器:

$('#idOfElement .classNameOfElements');

Or using find() : 或者使用find()

$('#idOfElement').find('.classNameOfElements');

Or using selector context: 或使用选择器上下文:

$('.classNameOfElements', '#idOfElement');

It's worth noting that using the context (final) approach causes jQuery to internally implement the find() method. 值得注意的是,使用上下文(final)方法会导致jQuery在内部实现find()方法。

References: 参考文献:

var byID = $("#someid");
var byClass = byID.find(".someClass");

In jquery you can get element by id as $('#some_id') and get element by class name as $('.some_class_id') please see jquery api for more details. 在jquery中,你可以通过id获取元素$('#some_id')并按类名获取元素$('.some_class_id')请参阅jquery api以获取更多详细信息。

and to access inside elements you can do it like this $('#some_id .some_class') 并且访问内部元素你可以这样做$('#some_id .some_class')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM