首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么onclick不使用innerHtml

问为什么onclick不使用innerHtml
EN

Stack Overflow用户
提问于 2017-06-08 05:35:10
回答 3查看 5.7K关注 0票数 1

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

代码语言:javascript
复制
  <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>

联署材料:

代码语言:javascript
复制
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没有被添加到元素中。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-08 05:51:43

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

代码语言:javascript
复制
<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文件将是。

代码语言:javascript
复制
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>';
}
票数 0
EN

Stack Overflow用户

发布于 2017-06-08 05:39:32

将innerHtml更改为innerHTML。

​

代码语言:javascript
复制
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>';
 }
代码语言:javascript
复制
<h3>Dynamically Retrieve the chapter name</h3>
<div>
<div>
    <button id="addChapter">Add Chapter</button>
    <div id="courseStructure">

    </div>
 </div>
 </div>

​

票数 0
EN

Stack Overflow用户

发布于 2017-06-08 05:42:33

您可以在js上选择:

代码语言:javascript
复制
$("#courseStructure").click(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>';
});

或者:

代码语言:javascript
复制
<button id="addChapter" onclick="myFunction()">Add Chapter</button>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44427385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档