【发布时间】:2011-05-29 16:22:39
【问题描述】:
我正在尝试使用 innerHTML 填充 div,它在除 IE 之外的所有浏览器下都运行良好(特别是在 8 和 9 下进行测试)。我可以找到很多关于这个主题的信息,但没有简单的答案。如果我在文本中没有任何标签,它似乎有时会起作用,但从来没有。谁能帮我理解这个问题以及可能的解决方法?
<html>
<body>
The following box should contain text populated with innerHTML,
but doesn't in IE
<p id="inner" style="width:250px; height: 100px; margin: 20px auto; background-color: #fff; border:5px solid orange; padding:7px; text-align:left; }"></p>
<script type="text/javascript">
var txt = "<h1>Just a quick test!</h1>"
var inner = document.getElementById("inner");
inner.innerHTML=txt;
</script>
</body>
</html>
【问题讨论】:
-
我知道。它在 IE 中不受支持。
-
错误
Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Tablet PC 2.0; InfoPath.2) Timestamp: Sun, 29 May 2011 16:28:57 UTC Message: Object doesn't support this property or method Line: 11 Char: 6 Code: 0 URI: http://eatyourpet.info/ietest/index.php -
不,这不是“不受支持的东西”。 innerHTML 是 IE 最先引入的一项功能。
-
但这也是我面临的错误......所以你能帮忙吗?
-
您不能在 P 标签内嵌套 H1 而不触发运行时错误。不过,您可以使用 SPAN 或其他内联元素。
标签: javascript internet-explorer innerhtml