First of all, we must correctly understand what child elements are. For example, we wrote a span tag on the web page. And write the text content in the span: "Welcome to me", then this text content is a child element of the span. Same, if the span is included in a div. Then span is the child element of the div. Look at the following code:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
From the above code, we can see that "Welcome to me" and span are both included in a div. But you cannot directly quote the text "Welcome to me" in the div. What I want to tell you is: when obtaining child elements, you cannot get them across levels. Child elements can only be referenced by direct parent elements! Similarly, this div is also considered a child element in the body tag. But you can't get the span tag directly in the body. You have to get the div in the body and then get the span. See the following example demonstration:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
I'll list several ways to get sub-elements in Dom:
The method to get the first child element in the current element is: firstChild
The method to get the last child element in the current element is: lastChild
The method to get all child elements in the current element is: childNodes
Tip: When processing child elements. You will encounter space problems. Because my code above is between body and div. There are no newlines between div and span, so this problem can be avoided. But you can't always ignore lines when writing code. A space element will form between rows in browsers such as FF. They will also treat these spaces as a valid element, see: Dom tricks for space filtering
Understand the sub-elements. Let’s talk about what the brother elements are. Literally you should be able to understand it almost. The so-called brother elements are actually called brother elements among elements with the same parent element. Look at the following code:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
The above code demonstrates: if you include elements such as div, span, a in a div tag, then these included div, span, a can be called brother elements for each other because they are all included by the same parent element!
Let's demonstrate how to get the elements between brothers:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
In the above code, we set an id attribute for the span element. Maybe you don't know yet, if you want to quickly get an element, you should set an id attribute for the element. Then use the getElementById method according to the value of the id attribute to obtain.
After obtaining the span element, we use the following Dom method to obtain the previous brother element div and the next brother element a of the span
Use the previousSibling method in Dom to get the previous sibling element of the current element
Use nextSibling method in Dom to get the next sibling element of the current element
There will also be space problems when obtaining sibling elements in FF browsing. Please see: Dom Skills for Space Filtering Through this article. You should be able to correctly understand or manipulate child elements and brother elements. If you need to reprint, be sure to keep the following information:
Copyright of this article: Web Circle First release address: http:///dom/dom_6.html
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
From the above code, we can see that "Welcome to me" and span are both included in a div. But you cannot directly quote the text "Welcome to me" in the div. What I want to tell you is: when obtaining child elements, you cannot get them across levels. Child elements can only be referenced by direct parent elements! Similarly, this div is also considered a child element in the body tag. But you can't get the span tag directly in the body. You have to get the div in the body and then get the span. See the following example demonstration:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
I'll list several ways to get sub-elements in Dom:
The method to get the first child element in the current element is: firstChild
The method to get the last child element in the current element is: lastChild
The method to get all child elements in the current element is: childNodes
Tip: When processing child elements. You will encounter space problems. Because my code above is between body and div. There are no newlines between div and span, so this problem can be avoided. But you can't always ignore lines when writing code. A space element will form between rows in browsers such as FF. They will also treat these spaces as a valid element, see: Dom tricks for space filtering
Understand the sub-elements. Let’s talk about what the brother elements are. Literally you should be able to understand it almost. The so-called brother elements are actually called brother elements among elements with the same parent element. Look at the following code:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
The above code demonstrates: if you include elements such as div, span, a in a div tag, then these included div, span, a can be called brother elements for each other because they are all included by the same parent element!
Let's demonstrate how to get the elements between brothers:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
In the above code, we set an id attribute for the span element. Maybe you don't know yet, if you want to quickly get an element, you should set an id attribute for the element. Then use the getElementById method according to the value of the id attribute to obtain.
After obtaining the span element, we use the following Dom method to obtain the previous brother element div and the next brother element a of the span
Use the previousSibling method in Dom to get the previous sibling element of the current element
Use nextSibling method in Dom to get the next sibling element of the current element
There will also be space problems when obtaining sibling elements in FF browsing. Please see: Dom Skills for Space Filtering Through this article. You should be able to correctly understand or manipulate child elements and brother elements. If you need to reprint, be sure to keep the following information:
Copyright of this article: Web Circle First release address: http:///dom/dom_6.html