3 回答
TA贡献1777条经验 获得超10个赞
循环项目,并使用insertAdjacentHTML将项目添加到正文或您想要添加的任何位置。
var fruits = ["bannanna", "apple", "orange", "grapes"]
fruits.forEach(item => document.body.insertAdjacentHTML('beforeend', `<h1>${item}</h1>`))
TA贡献1841条经验 获得超3个赞
你可以通过 forEach 来做到这一点。
<div id="generate-h1"></div>
var fruits = ["bannanna", "apple", "orange", "grapes"]
// Create any HTML Element to put H1 Tags into this HTML Tag
var main = document.createElement('main');
// Create H1 items for each fruits
// Append it to the main Tag or whatever you have.
fruits.forEach(function (fruits) {
var headline = document.createElement('h1');
headline.textContent = fruits;
main.appendChild(headline);
});
// Inject into the DOM
var app = document.querySelector('#generate-h1');
app.appendChild(main);
TA贡献1804条经验 获得超3个赞
好吧,试试这个,我相信这会起作用:
<!DOCTYPE html>
<html>
<head>
<title>Try This</title>
</head>
<body>
<h1 id="fruit"></h1>
<script>
var fruits = ["bannanna", "apple", "orange", "grapes"];
var text = "";
var i;
for (i = 0; i < fruits.length; i++) {
text += fruits[i] + "<br>";
}
document.getElementById("fruit").innerHTML = text;
</script>
</body>
</html>
- 3 回答
- 0 关注
- 110 浏览
添加回答
举报