【问题标题】:innerHtml cannot print my div inside anotherinnerHtml 无法在另一个内打印我的 div
【发布时间】:2016-12-16 18:38:22
【问题描述】:

我有一个以前从未遇到过的问题。我只想在另一个中添加一个 div。我首先使用开发者控制台进行测试。 我得到了要包含另一个 div 的元素,它在控制台中正确打印了该元素。 然后我执行以下操作:

document.getElementById("ctiGlobalContainer").innerHtml += '<div id="ctiContentPanel" state="displayed" style="z-index: 10000;right: 0;top: 50px;height: 100%;width: 200px;position: fixed;margin: 0;"></div>'

我得到以下结果:

"undefined<div id="ctiContentPanel" state="displayed" style="z-index: 10000;right: 0;top: 50px;height: 100%;width: 200px;position: fixed;margin: 0;"></div>"

为什么会显示 undefined ?并且 div 不包含在 dom 中...

【问题讨论】:

标签: javascript html


【解决方案1】:

该属性名为innerHTML

innerHtml 开始时未定义。

+= 将其转换为字符串,然后附加到它。

当您将undefined 转换为字符串时,您会得到"undefined"

【讨论】:

    【解决方案2】:

    你有错字。请使用innerHTML 代替innerHtmlJavascript 没有将 innerHtml 识别为其属性,这就是其中的代码显示 undefined 的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2022-01-12
      • 2017-10-23
      相关资源
      最近更新 更多