/
Coderdev
/
web-static-labs
Обзор
Документация
Войти
/
Coderdev
/
web-static-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
html/lab5/task2/script.js
102 строки
3 KB
Coderdev
1
30 мар 2026, 17:29
30 мар 2026, 17:29
04c9649
Код
Авторство
О чём код?
"use strict"; const taskText = document.getElementById("taskText"); const addTaskBtn = document.getElementById("addTaskBtn"); const taskList = document.getElementById("taskList"); const message = document.getElementById("message"); addTaskBtn.addEventListener("click", addTask); function addTask() { const text = taskText.value.trim(); if (!text) { message.textContent = "Введите текст задачи."; return; } message.textContent = ""; const taskItem = document.createElement("div"); taskItem.className = "task-item"; const taskDate = document.createElement("div"); taskDate.className = "task-date"; taskDate.textContent = getCurrentDateTime(); const taskContent = document.createElement("div"); taskContent.className = "task-text"; taskContent.textContent = text; const actions = document.createElement("div"); actions.className = "task-actions"; const copyBtn = document.createElement("button"); copyBtn.className = "copy-btn"; copyBtn.type = "button"; copyBtn.textContent = "Копировать в буфер"; const deleteBtn = document.createElement("button"); deleteBtn.className = "delete-btn"; deleteBtn.type = "button"; deleteBtn.textContent = "Удалить задачу"; copyBtn.addEventListener("click", function () { copyText(text); }); deleteBtn.addEventListener("click", function () { taskItem.remove(); }); actions.append(copyBtn, deleteBtn); taskItem.append(taskDate, taskContent, actions); taskList.prepend(taskItem); taskText.value = ""; } function getCurrentDateTime() { const now = new Date(); const day = String(now.getDate()).padStart(2, "0"); const month = String(now.getMonth() + 1).padStart(2, "0"); const year = now.getFullYear(); const hours = String(now.getHours()).padStart(2, "0"); const minutes = String(now.getMinutes()).padStart(2, "0"); const seconds = String(now.getSeconds()).padStart(2, "0"); return `${day}.${month}.${year} ${hours}:${minutes}:${seconds}`; } function copyText(text) { if (navigator.clipboard && window.isSecureContext) { navigator.clipboard.writeText(text) .then(function () { alert("Текст задачи скопирован."); }) .catch(function () { fallbackCopy(text); }); } else { fallbackCopy(text); } } function fallbackCopy(text) { const temp = document.createElement("textarea"); temp.value = text; document.body.appendChild(temp); temp.select(); temp.setSelectionRange(0, 99999); try { document.execCommand("copy"); alert("Текст задачи скопирован."); } catch (error) { alert("Не удалось скопировать текст."); } document.body.removeChild(temp); }