Use JavaScript to wrap text element nodes with a DIV, and use javascriptdiv

Source: Internet
Author: User

Use JavaScript to wrap text element nodes with a DIV, and use javascriptdiv

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 it on the parent element. the innerHTML attribute is used for update, but the problem is that all bound event listening will be invalid because an HTML element will be created again when innerHTML is used. 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 childNodes is a collection composed of dynamic nodes. Moving a node will affect its index value. We use the while loop to always check the firstChild of the parent element. If it returns a value that represents false, you will know that all nodes have been moved to the new parent!


Use javascript to create a new div element and insert it into the id = "parent" element as its subnode.

You can directly create a DIV

Var objDiv = document. getElementById ("parent"). innerHTML = "<div> </div> ";

Insert a pair of DIV tags directly under the ID of the Parent.

Another method is: (this method is what I saw on the Internet and I have never practiced)
JavaScript code
1. // create a div
2. var objDiv = document. createElement ("div"); // div created successfully
3. // add content to the div
4. objDiv. innerHTML = "javascript instance-tech house, welcome ";
5. // hide the Control div
6. objDiv. style. display = "none ";
7. // Control div display
8. objDiv. style. display = ""

I hope my answers will help you.
 
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>

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.