3 回答

TA貢獻1934條經驗 獲得超2個贊
所以我不確定你想要做什么,但看看這是否有幫助。這就是我對我認為您正在努力實現(xiàn)的目標的看法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="stylesheet" href="css/style.css">
<title>Vini Game Store</title>
</head>
<body>
<h1>Games</h1>
<div id = "game1"></br>
<div id = "game2"></br>
<div id = "game3">
<script>
let games = [];//array goes here
var game1 = document.getElementById('game1');
var game2 = document.getElementById('game2');
var game3 = document.getElementById('game3');
game1.innerHTML = games[0].title + ', price:' + games[0].price;
game2.innerHTML = games[1].title + ', price:' + games[1].price;
game3.innerHTML = games[2].title + ', price:' + games[2].price;
</script>
</body>
</html>

TA貢獻1860條經驗 獲得超8個贊
我將使用我通常會做的簡化版本,因為我覺得您是 javascript 新手。
我很喜歡將 HTML 和 javascript 分開。通常我會在 HTML 中創(chuàng)建一個模板,克隆它并使用它。我通常也會使用文檔片段,所以我只會更新一次 DOM。
為了保持簡單,我將使用模板文字并跳過文檔片段
var games = [{
title: 'God of War',
price: 50,
img: "./assets/images/God-of-War.jpg"
},
{
title: 'Death Stranding',
price: 70,
img: "./assets/images/Death-Stranding.jpg"
},
{
title: 'The Last Of Us 2',
price: 40,
img: "./assets/images/The-Last-Of-Us-2.jpg"
}
];
function buildGames(parent, games) {
var html = "";
games.forEach(function(game) {
//Set Our Itemp template
let itemTemplate = `
<li>
<h2>${game.title}</h2>
<div class="price">Price: $ ${game.price}</div>
<img src="${game.img}" alt="Image of ${game.title}" />
</li>`;
//update the html
html += itemTemplate
});
//Update the parent once
parent.innerHTML += html;
}
buildGames(document.getElementById("gamesList"), games);
#gamesList {
list-style: none;
padding-left: 0;
}
#gamesList li {
width: 200px;
display: inline-block;
border-radius: 15px;
box-shadow: 2px 2px 4px #333;
margin-right: 5px;
margin-top: 8px;
padding:10px;
}
<h1>Games</h1>
<ul id="gamesList">
</ul>
添加回答
舉報