insertAdjacentHTML Method
Inserts the given HTML text into the element at the location.
Syntax
object.insertAdjacentHTML(sWhere, sText)
Parameters
sWhere | Required. String that specifies where to insert the HTML text, using one of the following values:
beforeBegin | Inserts sText immediately before the object. | afterBegin | Inserts sText after the start of the object but before all other content in the object. | beforeEnd | Inserts sText immediately before the end of the object but after all other content in the object. | afterEnd | Inserts sText immediately after the end of the object. |
|
sText | Required. String that specifies the HTML text to insert. The string can be a combination of text and HTML tags. This must be well-formed, valid HTML or this method will fail. |
Return Value
No return value.
Remarks
If the text contains HTML tags, the method parses and formats the text as it is inserted.
You cannot insert text while the document is loading. Wait for the onload event to fire before attempting to call this method.
When using the insertAdjacentHTML method to insert script, you must include the DEFER attribute in the script element.
Example
This example uses the insertAdjacentHTML method to insert script into the page.
var sHTML="<input type=button οnclick=" +
"go2()" + " value='Click Me'><BR>"
var sScript='<SCRIPT DEFER>'
sScript = sScript +
'function go2(){ alert("Hello from inserted script.") }'
sScript = sScript + '</script' + '>';
ScriptDiv.insertAdjacentHTML("afterBegin",sHTML + sScript);
See Also
innerHTML, insertAdjacentText, outerHTML
object.insertAdjacentHTML(sWhere, sText)
在object标签内的指定位置插入HTML代码
sWhere:插入位置
如果object为<div>
beforeBegin 在object标签的前端 (在<div>之前)
afterBegin 在object标签里的最前端 (在<div>之后)
beforeEnd 在object标签里的最后端 (在</div>之前)
afterEnd 在object标签的后面 (在</div>之后)
sText 要插入的HTML代码
=======================================
C#
<%
@ Page language="c#" Codebehind="uploadFile.aspx.cs" AutoEventWireup="false" Inherits="OneForm.uploadFile"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
uploadFile
</
title
>
<
meta
content
="Microsoft Visual Studio 7.0"
name
="GENERATOR"
>
<
meta
content
="C#"
name
="CODE_LANGUAGE"
>
<
meta
content
="JavaScript"
name
="vs_defaultClientScript"
>
<
meta
content
="http://schemas.microsoft.com/intellisense/ie5"
name
="vs_targetSchema"
>
<
LINK
href
="css/css.css"
type
="text/css"
rel
="stylesheet"
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
script
language
="JavaScript"
>
function addFile()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var str = ' <INPUT type="file" size="50" NAME="File" class="box_m"><br>'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</
script
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=gb2312"
>
</
HEAD
>
<
body
bgColor
="#939ba2"
leftMargin
="4"
topMargin
="0"
>
<
table
width
="600"
align
="center"
>
<
tr
>
<
td
height
="18"
></
td
>
</
tr
>
<
tr
>
<
td
>
<
form
id
="FileUp"
method
="post"
encType
="multipart/form-data"
runat
="server"
>
<
table
class
="3dup"
height
="87"
cellSpacing
="0"
cellPadding
="0"
width
="460"
align
="center"
border
="0"
>
<
tr
>
<
td
colSpan
="3"
>
</
td
>
</
tr
>
<
tr
>
<
td
colSpan
="3"
>
<
div
align
="center"
>
上传录入文字文档
<
br
>
<
hr
>
</
div
>
</
td
>
</
tr
>
<
tr
>
<
td
height
="20"
>
<
div
align
="left"
><
FONT
face
="宋体"
>
项目名称:
</
FONT
></
div
>
</
td
>
<
td
colSpan
="2"
height
="20"
><
asp:label
id
="pname"
runat
="server"
ForeColor
="White"
></
asp:label
><
asp:label
id
="pid"
runat
="server"
Visible
="False"
></
asp:label
></
td
>
</
tr
>
<
tr
>
<
td
width
="69"
height
="20"
>
<
div
align
="left"
><
FONT
face
="宋体"
>
录
入
员:
</
FONT
></
div
>
</
td
>
<
td
width
="391"
colSpan
="2"
height
="20"
><
asp:label
id
="iman"
runat
="server"
ForeColor
="White"
></
asp:label
></
td
>
</
tr
>
<
tr
>
<
td
align
="middle"
colSpan
="3"
height
="20"
>
<
hr
>
<
input
class
="mybutton"
onclick
="addFile()"
type
="button"
value
="增加上传文件"
>
<
input
class
="mybutton"
id
="reset"
type
="reset"
value
="重新设置"
runat
="server"
>
<
asp:button
id
="Button1"
runat
="server"
CssClass
="mybutton"
Text
="立即上传"
></
asp:button
>
<
input
class
="mybutton"
onclick
="history.back();"
type
="button"
value
=" 返 回 "
name
="Button"
>
<
br
>
<
hr
>
<
P
id
="MyFile"
>
<
INPUT
class
="box_m"
type
="file"
size
="50"
name
="File"
>
<
br
>
</
P
>
</
td
>
</
tr
>
<
tr
>
<
td
colSpan
="3"
>
</
td
>
</
tr
>
<
tr
>
<
td
colSpan
="3"
></
td
>
</
tr
>
</
table
>
</
form
>
</
td
>
</
tr
>
</
table
>
</
body
>
</
HTML
>
注意:还有2个方法:
oElement = object.insertAdjacentElement(sWhere, oElement)
object.insertAdjacentHTML(sWhere, sText)
用法基本一样。