【发布时间】: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 中...
【问题讨论】:
-
使用 innerHTML 在页面中添加 HTML 不是最好的方法,请使用 jQuery 或参见
.appendChild()函数:stackoverflow.com/questions/14004117/…
标签: javascript html