Javascript 使用jQuery在另一个元素中按类名获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7010307/
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
Get elements by class name inside another element with jQuery
提问by mykola
How can I get an element by id, and then inside this element get all elements by class name using 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.
如何通过 id 获取元素,然后在该元素中使用 jQuery 按类名获取所有元素?使用标准的 JS 函数 getElementById() 和 getElementsByClassName() 很容易做到这一点,但不幸的是 IE 7-8 不支持后者。
回答by David says reinstate Monica
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:
参考:
回答by Dogbert
var byID = $("#someid");
var byClass = byID.find(".someClass");
回答by Aamir Adnan
In jquery you can get element by id as $('#some_id')
and get element by class name as $('.some_class_id')
please see jquery apifor 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')