我正在尝试使用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>
最佳答案
inner = document.getElementById("inner");
出错 - 如果将其更改为 var inner = document.getElementById("inner");
它会起作用的。
由于元素上的 id,IE 会自动创建一个全局 inner
变量 - 它看起来确实不喜欢您尝试为其分配内容。
关于javascript - innerHTML 在 IE 中无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168867/