insertAdjacentHTML and insertAdjacentText Methods

Source: Internet
Author: User

The DHTML object of IE provides four read-write properties to dynamically manipulate the contents of a page element: InnerText, Outertext, InnerHTML, outerhtml.

Two points to be aware of:

1. Where the value of the Innertext,outertext property is rendered as plain text, even if it contains an HTML tag, and innerHTML, outerHTML renders the text parsed by the HTML engine, It can react to the performance of HTML tags in attributes.

2. Assigning a value to an object's Outertext,outerhtml property (that is, a write operation) deletes the object.

The assignment of the above four attributes simply replaces the text content of the original object, and you want to add text content to the page to specify the relevant location of the element, using the insertAdjacentHTML and insertAdjacentText methods. The form is as follows:

Object.insertadjacenttext (Swhere, Stext)

Object.insertadjacenthtml (Swhere, Stext)

where Swhere represents the inserted text relative to the location of the HTML tag, there are four preset values:

Beforebegin,afterbegin,beforeend,afterend

The following points need to be noted in use:

1. Both methods must be used after the entire document has been loaded, or an error will occur.

2. insertAdjacentText can only insert plain text, insertadjacenthtml Insert HTML-formatted text

3. Insert script with insertadjacenthtml, you must use the Defer property in the script element, or the script execution will have a run-time error

4.InsertAdjacentHTML after inserting an HTML element, all and other possible collection of elements are automatically updated to respond to dynamic changes. such as the Sourceindex property of subsequent elements of the page will change.

5. When the Inserthtml/outerhtml property is given an invalid HTML tag, the method may have run-time errors. The following code will make an error:

<BODY>

<p id=pdiv></p>

<script language= "JavaScript" >

pdiv.innerhtml = "<p>hello</p>"

</SCRIPT>

</BODY>

In addition, the dynamic operation of the page content needs to pay attention to the following details:

1. Only content displayed within the body of the document can be dynamically changed by the above properties and methods, the contents of the body object can be dynamically manipulated, but the body object itself cannot be replaced.

2. The above properties and methods cannot manipulate empty tags (HTML tags without content), such as input,img.

3. For table objects, only TD (Innerhtml/innertext) and table (Outerhmtl/outertext) objects can replace or insert content with certain properties, while other table objects, such as TR, Tbody cannot use these attributes to change the content.

Adding HTML content and text content before using the innerHTML and InnerText methods, and recently found insertAdjacentHTML and insertAdjacentText methods, both of which are more flexible, You can insert HTML content and text content in the specified place. insertAdjacentHTML Method: Inserts an HTML tag statement at the specified place

Prototype: insertadajcenthtml (Swhere,stext)

Parameters:

Swhere: Specifies where the HTML tag statement is inserted, with four values available:
1. Beforebegin: Insert before the label starts
2. Afterbegin: After inserting the tag start tag
3. BeforeEnd: Before the tag end tag
4. Afterend: After inserting the tag end tag

Stext: What to insert

<script language= "JavaScript" >
function Myfun ()
{
var obj = document.getelementbyidx_x_x ("btn1");
Obj.insertadjacenthtml ("Afterend", "<br><inputname=/" txt1/">");
}
</script>

<body>
<input name= "TXT" >
<input id= "btn1" name= "btn1" type= "button" value= "More ..." onclick= "Myfun ()" >
</body>


=============================

<title>24.htminsertadjacenthtml inserting new content </title>
<script language= "JScript" >
function Addsome ()
{
Document.all.paral.insertAdjacentHTML ("Afterbegin", "Document.all.paral.insertAdjacentHTML ("BeforeEnd", "Document.all.paral.insertAdjacentHTML ("Beforebegin", "Document.all.paral.insertAdjacentHTML ("Afterend", "}
</script>
<body onload= "Addsome ()" >
<div id= "Paral" style= "fontsize:6;color= ' #ff00ff '" > Original content </div></body>


=================================

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.0transitional//en" >
<HTML>
<HEAD>
<TITLE> New document</title>
<meta name= "Generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
</HEAD><BODY>
<script>
var num=0;
var no_sys=0;function Add_button () {
if (no_sys<8) {
C_input.insertadjacenthtml ("BeforeEnd", "<divid=/" bar "+num+"/"oncontextmenu=/" Remove_button (bar "+num+"); returnfalse/"style=/" background:red;width:40;height:20/">" +num+ "</div>");
num++;
no_sys++;
}
}function Remove_button (obj) {
Obj.removenode (TRUE);
no_sys--;
}
</script>
<input type= "button" onclick= "Add_button ()" value= "Dynamic Plus" >
<input type= "button" onclick= "alert (c_input.innerhtml)" value= "See" >
<div id= "C_input" >
</div>
</BODY>
</HTML> usage:
<div id= "Test" >
<spanstyle= "Color:red" >test1</span>test2
</div>

In JS, you can use:

test.innerhtml:

That is, the entire contents of the object from its starting position to the terminating position, including the HTML tag.
The value of test.innerhtml in the example above is "<spanstyle=" color:red ">test1</span>test2".

Test.innertext:

The content from the starting position to the terminating position, but it removes the HTML tag
The value of text.innertest in the example above is "Test1 test2", where the span tag is removed.

test.outerhtml:

In addition to the entire contents of the innerHTML, the object label itself is included.
The value of text.outerhtml in the above example is <divid= "test" ><spanstyle= "color:red" >test1</span>test2</div>

Complete Example:
<div id= "Test" >
<spanstyle= "Color:red" >test1</span>test2
</div>

<ahref= "Javascript:alert (test.innerhtml)" >innerhtml content </a>
<ahref= "Javascript:alert (test.innertext)" >inerhtml content </a>
<ahref= "Javascript:alert (test.outerhtml)" >outerhtml content </a>

Special Note:

innerHTML is a standard-compliant attribute, and innertext only works with Internet Explorer, so use innerHTML as much as possible, and less innertext, if you want to output content that does not contain HTML tags, You can use innerHTML to get content that contains HTML tags, and then use regular expressions to remove the HTML tags, and here's a simple example of the standard:
<ahref= "Javascript:alert (document.getelementbyidx_x_x (' Test '). Innerhtml.replace (/<.+?>/gim, ')" > No HTML, meet the standards </a>

insertAdjacentHTML and insertAdjacentText Methods

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.