jQuery contents()Method

jQuery 遍历方法jQuery Traversal Methods

Example

Find all text nodes within the <div> element and wrap them with <b> elements:

$("div").contents().filter("em").wrap("<b/>");

Try it Yourself »

Definition and Usage

The contents() method returns all direct child elements, including text and comment nodes of the selected element.

Text nodes are the actual text displayed by an element.

This method is similar tochildren()The method is similar, except that it returns text and comment nodes. If in the same domain, the contents() method can also access the HTML of an iframe.


Syntax

$(selector).contents()


jQuery 遍历方法jQuery Traversal Methods

Other Extensions