dhtml provides two methods to add, insertAdjacentHTML and insertAdjacentText
insertAdjacentHTML method: Insert html tag statement in the specified place.
Prototype: insertAdjacentHTML(swhere,text)
parameter:
swe: Specifies the place where the html tag statement is inserted. There are four values that can be used:
:Insert before the label starts
: After inserting the tag start mark
:Insert before the end tag
:Insert to the end tag
line: What to insert
example:
var sHTML="<input type=button go2()" + " value='Click Me'><BR>"
var sScript='<SCRIPT DEFER>'
sScript = sScript + 'function go2(){ alert("Hello from inserted script.") }'
sScript = sScript + '</script' + '>';
("afterBegin",sHTML + sScript);
Add a line to the html text:
<DIV ID="ScriptDiv"></Div>
Finally it becomes:
<DIV ID="ScriptDiv">
<input type=button onclick=go2() value='Click Me'><BR>
<SCRIPT DEFER>
function go2(){alert("Hello from inserted sctipt.")}'
</script>
</DIV>
insertAdjacentText method is similar to insertAdjacentHTML method, except that it can only insert plain text and the parameters are the same
These two attributes are quite suitable, especially when used in drawings and other places. The advantage is that they will not cover the original content. Let's assume that there is a DIV that already contains content. Now we still need to dynamically add content, but cannot cover the original content. Then this thing is very important at this time. InnerHTML will overwrite the original content.
This attribute can be used for all pairs of HTML, which is the same as innerHTML. For example, <body>..</body>, <div>....</div> and other properties have these two attributes.
Added: I just tried it. The attribute innerHTML is readable and writeable. I used to know that innerHTML can insert content into nodes, but this attribute is also readable. In other words, the html content of the node is saved in innerHTML; after reading the following code, you will fully understand:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Unt titled document</title>
</head>
<body>
safdsdaf on time place
<script language="javascript">
alert()
</script>
</body>
</html>
The above is the code I reposted to someone else. I will add a few lines of code below. It is also very classic. Maybe you are using it:
<script language="javascript" type="text/javascript">
function addFile()
{
var filebutton = '<br><input type="file" size="50" name="File" />';
('FileList').insertAdjacentHTML("beforeEnd",filebutton);
}
</script>
The above is the script in the head, and the following is the html code in the body:
<p >
<input type="file" runat="server" size="50" name="File"/>
</p>
You will know the effect if you copy the code to a file and save it into an html.
insertAdjacentHTML method: Insert html tag statement in the specified place.
Prototype: insertAdjacentHTML(swhere,text)
parameter:
swe: Specifies the place where the html tag statement is inserted. There are four values that can be used:
:Insert before the label starts
: After inserting the tag start mark
:Insert before the end tag
:Insert to the end tag
line: What to insert
example:
Copy the codeThe code is as follows:
var sHTML="<input type=button go2()" + " value='Click Me'><BR>"
var sScript='<SCRIPT DEFER>'
sScript = sScript + 'function go2(){ alert("Hello from inserted script.") }'
sScript = sScript + '</script' + '>';
("afterBegin",sHTML + sScript);
Add a line to the html text:
<DIV ID="ScriptDiv"></Div>
Finally it becomes:
Copy the codeThe code is as follows:
<DIV ID="ScriptDiv">
<input type=button onclick=go2() value='Click Me'><BR>
<SCRIPT DEFER>
function go2(){alert("Hello from inserted sctipt.")}'
</script>
</DIV>
insertAdjacentText method is similar to insertAdjacentHTML method, except that it can only insert plain text and the parameters are the same
These two attributes are quite suitable, especially when used in drawings and other places. The advantage is that they will not cover the original content. Let's assume that there is a DIV that already contains content. Now we still need to dynamically add content, but cannot cover the original content. Then this thing is very important at this time. InnerHTML will overwrite the original content.
This attribute can be used for all pairs of HTML, which is the same as innerHTML. For example, <body>..</body>, <div>....</div> and other properties have these two attributes.
Added: I just tried it. The attribute innerHTML is readable and writeable. I used to know that innerHTML can insert content into nodes, but this attribute is also readable. In other words, the html content of the node is saved in innerHTML; after reading the following code, you will fully understand:
Copy the codeThe code is as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Unt titled document</title>
</head>
<body>
safdsdaf on time place
<script language="javascript">
alert()
</script>
</body>
</html>
The above is the code I reposted to someone else. I will add a few lines of code below. It is also very classic. Maybe you are using it:
Copy the codeThe code is as follows:
<script language="javascript" type="text/javascript">
function addFile()
{
var filebutton = '<br><input type="file" size="50" name="File" />';
('FileList').insertAdjacentHTML("beforeEnd",filebutton);
}
</script>
The above is the script in the head, and the following is the html code in the body:
Copy the codeThe code is as follows:
<p >
<input type="file" runat="server" size="50" name="File"/>
</p>
You will know the effect if you copy the code to a file and save it into an html.