Зміна структури та стилів
Мета: Навчитися змінювати структуру HTML-документа та стилізувати елементи через JS.
1. Зміна тексту та атрибутів
const title = document.querySelector("h1");
title.textContent = "Ласкаво просимо!";
title.setAttribute("data-id", "welcome");
2. Додавання й видалення елементів
const list = document.querySelector("ul");
const newItem = document.createElement("li");
newItem.textContent = "Блог";
list.appendChild(newItem);
list.removeChild(list.firstElementChild);
3. Зміна HTML-вмісту
const container = document.querySelector(".content");
container.innerHTML = "<p><strong>Оновлений</strong> текст!</p>";
4. Стилі та класи
const box = document.querySelector(".box");
box.style.backgroundColor = "lightblue";
box.classList.add("active");
box.classList.toggle("highlight");
5. Приклад
<button id="add">Додати</button>
<ul id="tasks"></ul>
const button = document.getElementById("add");
const list = document.getElementById("tasks");
button.addEventListener("click", () => {
const li = document.createElement("li");
li.textContent = "Нове завдання";
li.classList.add("task");
list.appendChild(li);
});