---
title: "设置iframe的document.designMode后仅Firefox中其body.innerHTML为br"
author: "zhouyrt"
date: "2012-02-14T14:30:13Z"
tags: []
categories: ["Web前端"]
slug: "iframe-document-designmode-firefox-body-innerhtml-br"
draft: false
lastmod: "2012-02-14T14:30:13Z"
id: 1405511
url: "https://snandy.iteye.com/blog/1405511"
---

设置iframe的document.designMode为On可以让其可编辑，一般用在富文本编辑器组件中。这里仅列出各浏览器差异。重现如下：

 

```
<!doctype html>
<html>
	<head>
		<title>设置iframe的document.designMode后仅Firefox中其body.innerHTML为br</title>
		<meta charset="utf-8">
 	</head>
 	<body>
		<iframe frameborder="1" style="height: 330px;"></iframe>
		<script>
			var ifr = document.getElementsByTagName('iframe')[0];
			var doc = ifr.contentWindow.document;
			function prif() {
				console.log(ifr.contentWindow.document.body.innerHTML);
			}
			
			function changeDesignMode() {
				ifr.contentWindow.document.designMode = 'On';
			}
			
			prif();
		</script>
 	</body>
</html>
```

 

以上代码iframe的body中没有写入任何html标记。正常情况下输出ifr.contentWindow.document.body.innerHTML应该是空字符串。但Firefox中有点特殊。

 

请按下面步骤依次操作。

 

Firefox中打开该html默认输出了空字符串

![](http://dl.iteye.com/upload/attachment/0063/1611/de8e53a2-f7fe-3de8-b6ba-0bb670598233.png)

 

控制台中执行changeDesignMode方法，再执行prif方法，这时输出的innerHTML为“&lt;br&gt;”，如下

![](http://dl.iteye.com/upload/attachment/0063/1620/2de80148-451b-3d35-8e12-2086edff6111.png)

 

 

 

Chrome/Safari/Opera输出的仍然是空字符串。