<body>
<div id = "app">TODO... This is an HTML5 Template. Put your own content here.</div>
<script>
var Para = prompt("How many paragraph elements you want?");
var element;
for(i = 0; i < Para; i++){
element = prompt("Provide the element you want to fill up the Paragraph above");
}
document.getElementById("app").innerHTML = element;
</script>
</body>
答案 0 :(得分:0)
您应该这样做:
document.getElementById("app").insertAdjacentHTML('beforeend', element);
正如@Makyen所说,您不应该像我在第一个答案中所说的那样使用innerHTML +=
。这可能会在您的应用程序中造成问题,请按照评论中的链接获取更多信息。
也许,我编写的代码行应该在for
循环内。
for(i = 0; i < Para; i++){
element = prompt("Provide the element you want to fill up the Paragraph above");
document.getElementById("app").insertAdjacentHTML('beforeend', element);
}