Use JavaScript to wrap text element nodes and javascript nodes

Source: Internet
Author: User

Use JavaScript to wrap text element nodes and javascript nodes
Original article link: Wrapping Text Nodes and Elements with JavaScript original Article Date: Translated by: Tie
When your application depends on a specific JavaScript class library, you will always try to solve the problems of some class libraries rather than the language. For example, when I try to wrap text (which may also contain HTML elements) with a DIV element. Suppose there are the following HTML:

This is some text and <a href="">a link</a>.

At this time, if you want to convert it to the following:
<div>This is some text and <a href="">a link</a>.<div>

The simplest and most violent method is that you can use . InnerHTMLAttribute to execute the update, but the problem is that all bound event listening will be invalid because InnerHTMLA new HTML element is created. This is a big glass! Therefore, JavaScript can only be used for implementation at this time-the size is limited and the size is limited. The following is the implementation code:
Var newWrapper = document. createElement ('div '); while (existingParent. firstChild) {// move the DOM element, no new element newWrapper. appendChild (existingParent. firstChild) is created );}

The for loop cannot be used here, because ChildNodesIs a collection composed of dynamic nodes. Moving a node will affect its index value. We use WhileCyclically checks the parent element's FirstChildIf it returns a value that represents false, you will know that all nodes have been moved to the new parent!

How does javascript obtain the node text value?

GetElementsByTagName ("tag name") indicates that the query returns all elements of the tag. Therefore, it is a set.
Document. getElementsByTagName ("tag") is used to query all the tag elements in the document.
Alert (document. getElementsByTagName ("div"). length) returns the number of query results. Here the result is 1
The text of this element should be written in document. getElementsByTagName ("div") [0]. innerHTML.
Take the html text of the first div

======================================
Other examples
<Html>
<Head> <title> test </title> <Script type = "text/javascript">
// Search for the span subnode under the first div in the document
Window. onload = function () {// document loading completion event
Var div1 = document. getElementsByTagName ("div") [0]; // The first DIV of the document
Var spans = div1.getElementsByTagName ("span"); // All SPAN tag elements under the DIV
For (var I = 0; I <spans. length; I ++) {// The text of each SPAN label is displayed cyclically.
Alert (spans [I]. innerHTML );
}
}
</Script>
<Body>
<Div> <span> text 1 </span> <span> second text </span> <span> text </span> </div>
</Body>
</Html>

In javascript, nodetype has three nodes: 1 representing the element node, 2 representing the attribute node, and 3 representing the text node.

NodeType is the type used to obtain the object of the current node.

The nodeType attribute returns the node type.
Element 1
Attribute attr 2
Text 3
Comment comments 8
Document 9

For example
Html:

Js: var element = document. getElementById ("h1"); // h1 is a Var type = element. nodeType; // The returned nodeType = 1, type = 1.

And so on

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.