为什么 onclick 不适用于innerHtml

基兰

在这里,我尝试使用 innerHtml 将 html 添加到元素

  <body>
 <h3>Dynamically Retrieve the chapter name</h3>
<div>
<div>
    <button id="addChapter">Add Chapter</button>
    <div id="courseStructure">

    </div>
 </div>
 </div>
<script src="index.js"></script>
</body>

JS:

var addChapter = document.getElementById("addChapter");

  addChapter.onclick = function(){
document.getElementById("courseStructure").innerHtml += '<div><input type="text" name="chaptername" placeholder="chapter name"/><button id="addSubChapter">Add SubChapter</button><button id="addContent">Add Content</button><br><br></div>';
 }

一切似乎都很好 onclick 事件在单击按钮时触发,但 html 没有被添加到元素中。

维卡斯·苏里亚万什

您没有以正确的格式编写innerHTML。它的innerHTML而不是innerHtml你的代码应该是..

<body>
<h3>Dynamically Retrieve the chapter name</h3>
<div>
<div>
<button id="addChapter">Add Chapter</button>
<div id="courseStructure">

</div>
</div>
</div>
<script src="index.js"></script>
</body>

您的 .JS 文件将是。

var addChapter = document.getElementById("addChapter");

addChapter.onclick = function()
{
document.getElementById("courseStructure").innerHTML += '<div><input type="text" name="chaptername" placeholder="chapter name"/>
<button id="addSubChapter">Add SubChapter</button>
<button id="addContent">Add Content</button>
<br><br></div>';
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章