Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

element.innerHTML

这篇文章需要文法复核。如何帮忙。

这篇翻译不完整。请帮忙从英语翻译这篇文章

摘要

innerHTML 属性可以用来获取、修改指定元素内的所有标签和内容。

语法

var markup = element.innerHTML;
element.innerHTML = markup;
  • 在上例中markup是包含元素(以及子元素)内容的字符串。元素的内容以原始HTML代码显示出来。 例如 "<p>Some text</p>".

示例

// HTML:
// <div id="d"><p>Content</p>
// <p>Further Elaborated</p>
// </div>

d = document.getElementById("d");
dump(d.innerHTML);

// the string "<p>Content</p><p>Further Elaborated</p>" 
// is dumped to the console window

注意

尽管innerHtml属性不属于W3C DOM规范,但是它为完全替换元素内容提供了一个更加便捷的方式。举个例子,可以通过如下代码完全删除文档内body的内容:

document.body.innerHTML = "";  // Replaces body content with an empty string.

包括BODY和HTML在内的许多类型元素的innerHTML属性可以被获取或者替换。当一个页面内容被动态修改之后,可以使用它来观察源代码。

// Copy and paste into address bar as a single line
javascript:x=document.body.innerHTML.replace(/</g,'&lt;').replace(/\n/g,'<br>'); document.body.innerHTML = x;

As there is no public specification for this property, implementations differ widely. For example, when text is entered into a text input, IE will change the value attribute of the input's innerHTML property but Gecko browsers do not.

It should never be used to write parts of a table—W3C DOM methods should be used for that—though it can be used to write an entire table or the contents of a cell.

规范

DOM Level 0 不属于任何标准.

参考

MSDN innerHTML

其它

  • innerDOM - For those wishing to adhere to standards, here is one set of JavaScript functions offering to serialize or parse XML so as to set element contents defined as string(s) via the DOM or getting element contents obtained from the DOM as a string.

文档标签和贡献者

 此页面的贡献者: RyanPan, teoli, khalid32, ziyunfei, zezhou, 巴别塔工人
 最后编辑者: RyanPan,