Differences between contents () and children () in jQuery

Source: Internet
Author: User
Tags processing instruction


1. Basic Concepts

First of all, there is a difference between the two methods.

The Ontents () method returns all the immediate child elements, including the text and annotation nodes of the selected element.

The text node is the actual text that is displayed by the element.

The method is similar to the children () method, but it returns the text and annotation nodes. If in the same domain, the contents () method can also access the HTML of the IFRAME

Children ()

The. Children (selector) method returns all the child elements of each element in the matching element collection (only the Son generation). Parameters are optional, and adding parameters means filtering through the selector to filter the elements.


So, children is a subset of contents.

2, text node

Each node has properties that contain some information about the node. These properties are:

NodeName (node name)
NodeValue (node value)
NodeType (node type)
Valid node types include the following:

1-element (Element)
2-attribute (properties)
3-text (text)
4-cdata
5-entity REFERENCE
6-entity
7-PI (processing instruction)
8-comment (note)
9-document (document)
10-document TYPE
11-document FRAGMENT
12-notation

For example:

<script>
$ (Functions ()
{
$ (' #cc '). Contents (). filter (function ()
{
Return this.nodetype==3;//text node
}). Wrap (' <strong style= ' color:red; > ');
});
</script>
<div id= "CC" >
<br/>
<br/>
Some
<br/>
</div>

The results are:

The result cannot be made out of children.

3, Summary

Same point: You can get a subset of the elements.
Different points: Contents gets more of the text nodes and elements within the frame.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.