JavaScript application Analysis for DOM (ii) _dom

Source: Internet
Author: User
In fact in the native also JS also has these attributes. It's almost the same as JQ but less than JQ. But it's a little more troublesome to use than JQ. Mainly because FF browser, because FF will take your line as a DOM element. For example
Copy Code code as follows:

<div id = "Dom" >
<div></div>
<div></div>
</div>

I use native JS to get the child element under the element with the ID dom. In my first chapter, the method is var a = document.getElementById ("Dom"). getElementsByTagName ("div"); that's fine. The alert (A.LENGTH) hint will be 2, but now we can get a different approach to the var B = document.getElementById ("Dom") that I mentioned in the previous chapter. ChildNodes; If this alert (b.length) IE browser is no problem or 2, but in the FF browser will be prompted is 4, this is because FF also as an element of the newline.
So we have to deal with those attributes that can be used with JS. The process of thinking is very simple to traverse these elements. The element type is a space and the text is deleted. The processing function is like this
Copy Code code as follows:

function Del_space (elem) {
var elem_child = elem.childnodes;
for (Var i=0;i<elem_child.length;i++) {
if (Elem_child.nodename = = "#text" &&!/\s/.test (Elem_child.nodevalue))
{Elem.removechild (Elem_child)}
}}

Let me explain this function.
var elem_child = elem.childnodes;
Throw the elem elements of the incoming element into the elem_child;
Copy Code code as follows:

for (Var i=0;i<elem_child.length;i++) {
if (Elem_child.nodename = = "#text" &&!/\s/.test (Elem_child.nodevalue))
{Elem.removechild (Elem_child)}
}

Traverse these child elements. If the node type inside these elements is text and the node value of the text type node is empty. Just delete it.

(NodeName is a property in JS, get this node type,/\s/this is not empty characters in JS normal expression.) Adding an exclamation point to the front indicates that it is a null character. Test is a method of JS, which is to compare the contents of it and the outside things. NodeValue means to get the value in this node removechild is also a way to delete a child element of the outer element.

This only needs to be called before these properties are called. Clean up the blanks and you'll be relieved.
Copy Code code as follows:

<div id = "Dom" >
<div></div>
<div></div>
</div>

<script>
function dom () {
var a = document.getElementById ("Dom");
Del_space (a); Call a function that clears the space
var B = a.childnodes; gets all the child nodes of A;
var c = A.parentnode; Gets the parent node of A;
var d = a.nextsbiling; Gets the next sibling node of a
var e = a.previoussbiling; Gets the previous sibling node of a
var f = a.firstchild; Gets the first child node of a
var g = A.lastchild; Gets the last child node of a

}
</script>

(another.) var B = a.childnodes; gets an array too; So for example I want to use the first node is childnodes[0]; I want to use the second node is childnodes[1];

It's over here to get the DOM. The next chapter teaches you how to manipulate DOM elements.
Related Article

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.