How to copy or insert Html in js _ javascript skills

Source: Internet
Author: User
In many cases, innerHTML or innerText is used. Currently, direct dom operations are popular. You can choose one as needed. Var bq = document. getElementsByTagName ("tag or ID name") // ID is a unique and non-unique group. A tag may return an array.
P = document. createElement ("p ");
... Set the CSS style
Bq. appendChild (p); // If the ID is unique, you only need this! Insert p layer after bq
Bq [0]. appendChild (p); // If the tag is used before! Array and subscript! Insert p layer after bq
Bq. insertBefore (p); // If the ID is unique, you only need this! Insert p Layer Before bq
Bq [0]. insertBefore (p); // If the tag is used before! Array and subscript! Insert p Layer Before bq

Document.getelementbyid('navition'developer.style.css Text = 'your CSS Code ';
// Copy a p
Var bq = document. getElementById ("Copy") // ID is a unique and non-unique group. The tag is not unique and an array may be returned.
ObjDiv = document. createElement ("p ");
ObjDiv. id = bq. id + Copy;
ObjDiv. style. position = "absolute"
ObjDiv. style. left = "200px"
ObjDiv. style. top = "200px"
ObjDiv. innerHTML = bq. innerHTML;
Bq. appendChild (objDiv );


Test1 test2



Test1 test2




In JS, you can use:

Test. innerHTML: All content from the starting position of the object to the ending position, including the Html Tag.
In the preceding example, the value of test. innerHTML is

Test1 test2
Test1 test2


Test. innerText: content from the starting position to the ending position, but it removes the Html Tag
In the preceding example, the value of text. innerTest is "test1 test2", in which the span tag is removed.


Test. outerHTML: not only contains all the content of innerHTML, but also contains the object tag itself.
In the preceding example, the value of text. outerHTML is

Test1 test2


Test1 test2




Complete example:

The Code is as follows:



Test1 test2



InnerHTML content
InerHTML content
OuterHTML content


Test1 test2



InnerHTML content
InerHTML content
OuterHTML content



Note:

InnerHTML is a W3C standard attribute, while innerText is only applicable to IE browsers. Therefore, try to use
InnerHTML, rather than innerText. to output content without HTML tags, you can use innerHTML to obtain
After the content of the HTML Tag is removed using a regular expression, the following is a simple example that complies with W3C standards:

The Code is as follows:



Test1 test2


/Gim, '')"> no html, compliant with W3C standards


Test1 test2


/Gim, '')"> no html, compliant with W3C standards


Related Articles
Javascript dom operations details js enhancements
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.