---
title: "js中innerHTML与innerText的用法与区别 "
author: "Vionsam"
date: "2012-02-20T15:50:44Z"
tags: ["JavaScript", "正则表达式innerHTML"]
categories: ["移动开发"]
slug: "js-innerhtml-innertext"
draft: false
lastmod: "2012-02-20T15:50:44Z"
id: 1413656
url: "https://vionsam.iteye.com/blog/1413656"
---

用法：

test1 test2

在JS中可以使用： test.innerHTML: 　　也就是从对象的起始位置到终止位置的全部内容,包括Html标签。 　　上例中的test.innerHTML的值也就是“test1 test2 ”。 test.innerText: 　　从起始位置到终止位置的内容, 但它去除Html标签 　　上例中的text.innerTest的值也就是“test1 test2”, 其中span标签去除了。 test.outerHTML: 　　除了包含innerHTML的全部内容外, 还包含对象标签本身。 　　上例中的text.outerHTML的值也就是

test1 test2

完整示例：

test1 test2

[innerHTML内容](javascript:alert%28test.innerHTML%29) [inerHTML内容](javascript:alert%28test.innerText%29) [outerHTML内容](javascript:alert%28test.outerHTML%29) 特别说明： 　　innerHTML是符合W3C标准的属性，而innerText只适用于IE浏览器，因此，尽可能地去使用innerHTML，而少用innerText，如果要输出不含HTML标签的内容，可以使用innerHTML取得包含HTML标签的内容后，再用正则表达式去除HTML标签，下面是一个简单的符合W3C标准的示例： [无HTML,符合W3C标准](javascript:alert%28document.getElementById%28'test'%29.innerHTML.replace%28/%3C.+?%3E%2Fgim%2C%27%27%29%29)