Sets or retrieves the HTML between the start and end tags of the object. dhtml语法 HTML | N/A |
---|
Scripting | object.innerHTML [ = sHTML ] |
---|
DHTML可能的值 sHTML | String that
specifies or receives the content between the start and end tags. |
The property is read/write
for all objects except the following, for which it is read-only:
col, colgroup, frameset, html, style, table, tbody, tfoot, thead, title, TR.
The property has no default value. Expressions can be used in place of the preceding value(s), as of Microsoft® Internet Explorer 5. For more information, see about dynamic properties.
remarks the innerhtml property is valid for both block and inline elements. by definition, elements that do not have both an opening and closing tag cannot have an innerhtml property. the innerhtml property takes a string that specifies a valid combination of text and elements. when the innerhtml property is set, the given string completely replaces the existing content of the object. if the string contains html tags, the string is parsed and formatted as it is placed into the document. this property is accessible at run time, as of microsoft® internet explorer 5. removing elements at run time, before the closing tag is parsed, could prevent other areas of the document from rendering. when using innerhtml to insert script, you must include the defer attribute in the script element. you can change the value of the title element using the document.title property. to change the contents of the table, tfoot, thead, and tr elements, use the table object model described in how to build tables dynamically. for example, use the rowindex property or the rows collection to retrieve a reference to a specific table row. you can add or delete rows using the insertrow and deleterow methods. to retrieve a reference to a specific cell, use the cellindex property or the cells collection. you can add or delete rows using the insertcell and deletecell methods. to change the content of a particular cell, use the innerhtml property. to maintain compatibility with earlier versions of internet explorer, this property applies to the textarea object. however, the property works only with strings that do not contain tags. with a string that contains a tag, this property returns an error. it is better to use the innertext property with this object.
Examples
This example uses the innerHTML property to change the text of a paragraph when an onmouseover event occurs. the affected text and any tags within it are changed by the onmouseover and onmouseout events. ...
<P onmouseover="this.innerHTML='<B>Mouse out to change back.</B>'"
onmouseout="this.innerHTML='<I>Mouse over again to change.</I>'">
<I>Mouse over this text to change it.</I>
</P>
... This feature requires Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.
This example uses the innerHTML property to insert script into the page. <HTML>
<SCRIPT>
function insertScript(){
var sHTML="<input type=button onclick=" + "go2()" + " value='Click Me'><BR>";
var sScript="<SCRIPT DEFER>";
sScript = sScript + "function go2(){ alert('Hello from inserted script.') }";
sScript = sScript + "</SCRIPT" + ">";
ScriptDiv.innerHTML = sHTML + sScript;
}
</SCRIPT>
<BODY onload="insertScript();">
<DIV ID="ScriptDiv"></DIV>
</BODY>
</HTML> This feature requires Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.
是否符合公共标准
There is no public standard that applies to this property.
Applies To |
A,
ACRONYM,
ADDRESS,
B,
BDO,
BIG,
BLOCKQUOTE,
BODY,
BUTTON,
CAPTION,
CENTER,
CITE,
CODE,
COL,
COLGROUP,
CUSTOM,
DD,
DEL,
DFN,
DIR,
DIV,
DL,
DT,
EM,
FIELDSET,
FONT,
FORM,
FRAMESET,
hn,
HTML Comment,
HTML,
I,
INS,
KBD,
LABEL,
LEGEND,
LI,
LISTING,
MAP,
MARQUEE,
MENU,
nextID,
NOBR,
OL,
OPTION,
P,
PRE,
Q,
RT,
RUBY,
S,
SAMP,
SCRIPT,
SELECT,
SMALL,
SPAN,
STRIKE,
STRONG,
STYLE,
SUB,
SUP,
TABLE,
TBODY,
TD,
TFOOT,
TH,
THEAD,
TITLE,
TR,
TT,
U,
UL,
VAR | |
更多语法参考 insertadjacenthtml, How to Build Tables Dynamically
|