gpt4 book ai didi

javascript - 将数组返回的项目追加到 html

转载 作者:行者123 更新时间:2023-12-03 07:36:21 25 4
gpt4 key购买 nike

下面的函数 getRandom() 工作正常并且返回数组的随机项。如何将返回项目 append 到我的 DOM 中?我尝试了几种方法 append 子节点并创建文本节点,但无法使其工作。

我想做的是将 getRandom append 到我的 html 中。

// 'use strict';

var button;
var randomButton;
var myArray = [];
var list = document.getElementById("greatList");
var item = document.getElementById("addItems").value;
//console.log(item);

document.addEventListener('DOMContentLoaded', init);

function init(){
button = document.getElementById('addButton');


document.querySelector('#addButton').addEventListener(addField);
document.querySelector('#random_button').addEventListener(getRandom);
//button.addEventListener('click', getItem)

}

function addField(){
var item = document.getElementById("addItems").value;
var items = item.split(',');
for (var i=0; i < items.length; i++){
var newField = document.createElement('li');
newField.appendChild(document.createTextNode(items[i]));
list.appendChild(newField);
myArray.push(item);
}
}

function getRandom(){

var randomize = myArray[Math.floor(Math.random() * myArray.length)];
var newRandomItem = document.getElementById("newRandom");
newRandomItem.appendChild(randomize);

return randomize;
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HomeWork 1 - Add Name to list</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<div class="list">
<h2>List: </h2>
<section class="top">

<input type="text" multiple="multiple" id="addItems">
<button type="submit" id="addButton" onclick="addField()">Add</button>
<button id="random_button">Get Random</button>
</section>
<hr>
<section class="result">
<ul id="greatList">
<li class="singleLine">Hello</li>
</ul>
</section>
<section>
<ul >
<li id="newRandom"></li>
</ul>
</section>
</div>
</div>
<script src="app.js"></script>
</body>
</html>

最佳答案

您可以使用innerHTML。请注意,您的数组中不存在“Hello”。

// 'use strict';

var button;
var randomButton;
var myArray = [];
var list = document.getElementById("greatList");
var item = document.getElementById("addItems").value;
//console.log(item);

document.addEventListener('DOMContentLoaded', init);

function init(){
button = document.getElementById('addButton');


document.querySelector('#addButton').addEventListener(addField);
document.querySelector('#random_button').addEventListener('click', getRandom);
//button.addEventListener('click', getItem)

}

function addField(){
var item = document.getElementById("addItems").value;
var items = item.split(',');
for (var i=0; i < items.length; i++){
var newField = document.createElement('li');
newField.appendChild(document.createTextNode(items[i]));
list.appendChild(newField);
myArray.push(item);
}
}

function getRandom(){

var randomize = myArray[Math.floor(Math.random() * myArray.length)];
var newRandomItem = document.getElementById("newRandom");
if(randomize != undefined) {
newRandomItem.innerHTML = randomize;

return randomize;
}
else {
return false;
}

};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HomeWork 1 - Add Name to list</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<div class="list">
<h2>List: </h2>
<section class="top">

<input type="text" multiple="multiple" id="addItems">
<button type="submit" id="addButton" onclick="addField()">Add</button>
<button id="random_button">Get Random</button>
</section>
<hr>
<section class="result">
<ul id="greatList">
<li class="singleLine">Hello</li>
</ul>
</section>
<section>
<ul >
<li id="newRandom"></li>
</ul>
</section>
</div>
</div>
<script src="app.js"></script>
</body>
</html>

关于javascript - 将数组返回的项目追加到 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com