javascript - innerHTML 在 IE 中无法正常运行

标签 javascript internet-explorer innerhtml

我正在尝试使用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/

相关文章:

javascript - 我的 if else 语句未按预期工作 - 所有语句的innerHTML 保持不变

javascript - 如何使用 JavaScript 将 JSON 字符串中的键和值添加到 innerHTML 中

javascript - 选择具有相同类的多个div

javascript - Rails AJAX 远程 : true with dynamic id

css - Gmail 电子邮件的 Internet Explorer 特定媒体查询或破解

javascript - 重复 XML 元素在 Internet Explorer 中不起作用

ajax - Long Poll 和 IE 的 XDomainRequest 对象

来自现有 divs innerHTML 的 javascript 数组

javascript - 将数组的项映射到另一个数组

javascript - HTML5 video.currentTime 设置不正确