English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The children() method returns all direct child elements of the selected element.
Note: The children() method only traverses a single level of the DOM tree. To traverse multiple levels, please usefind()method.
The children() method is similar tocontents()Method, the difference is that it does not return text and comment nodes.
$(selector).children(selectorExpression)
Return the element that is a direct child of DIV:
$("document").ready(function(){ $("div").children().css("background-color", "coral"); });Test and see‹/›
Return all direct children of the DIV:
$("document").ready(function(){ $("div").children("p").css("background-color", "coral"); });Test and see‹/›
Return all child elements of the clicked element:
$("#container").click(function(event){ $("*).removeClass("hilite"); let kids = $(event.target).children(); let len = kids.addClass("hilite").length; $("#results span:first").text(len); $("#results span:last").text(event.target.tagName); event.preventDefault(); });Test and see‹/›
The children() method only traverses a single level of the DOM tree downwards. The following example returns the direct child element of div id="box":
p (grandchild level) span (great-grandchild level)
Parameter | Description |
---|---|
selectorExpression | Specify a selector expression to match elements (optional) |