Сделал заданьку
This commit is contained in:
31
script.js
Normal file
31
script.js
Normal file
@@ -0,0 +1,31 @@
|
||||
// Ваш код здесь
|
||||
|
||||
// Находим блок, куда будем вставлять карточки
|
||||
const container = document.getElementById('cards');
|
||||
|
||||
// Делаем запрос к API
|
||||
fetch('https://jsonplaceholder.typicode.com/posts')
|
||||
.then(response => response.json()) // Превращаем ответ в понятный массив
|
||||
.then(data => {
|
||||
// Отрезаем первые 5 элементов
|
||||
const limitedPosts = data.slice(0, 5);
|
||||
|
||||
// Перебираем массив циклом
|
||||
for (let i = 0; i < limitedPosts.length; i++) {
|
||||
const post = limitedPosts[i];
|
||||
|
||||
// Создаем HTML-разметку в виде строки
|
||||
const cardHtml = `
|
||||
<div class="card">
|
||||
<h2>${post.title}</h2>
|
||||
<p>${post.body}</p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// Добавляем эту строку в контейнер
|
||||
container.innerHTML += cardHtml;
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('Произошла ошибка:', error);
|
||||
});
|
||||
Reference in New Issue
Block a user