/
aska
/
web-static-labs
Обзор
Документация
Войти
/
aska
/
web-static-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
html/lab5/task2/script.js
96 строк
3 KB
unknown
лабы 1 4 5
14 ноя 2025, 09:16
14 ноя 2025, 09:16
b757c12
Код
Авторство
О чём код?
document.addEventListener('DOMContentLoaded', function() { const taskInput = document.getElementById('taskInput'); const addTaskButton = document.getElementById('addTaskButton'); const taskList = document.getElementById('taskList'); addTaskButton.addEventListener('click', function() { const taskText = taskInput.value.trim(); if (taskText !== "") { addTask(taskText); taskInput.value = ""; } }); function addTask(taskText) { const taskDiv = document.createElement('div'); taskDiv.classList.add('task'); const now = new Date(); const dateTimeString = now.toLocaleString(); taskDiv.innerHTML = ` <div class="task-text">${dateTimeString} - ${taskText}</div> <div class="task-buttons"> <button class="copyButton">Копировать в буфер</button> <button class="deleteButton">Удалить задачу</button> </div> `; taskList.prepend(taskDiv); taskDiv.addEventListener('mouseover', highlightTask); taskDiv.addEventListener('mouseout', unhighlightTask); const copyButton = taskDiv.querySelector('.copyButton'); copyButton.addEventListener('click', function() { copyToClipboard(taskText); }); const deleteButton = taskDiv.querySelector('.deleteButton'); deleteButton.addEventListener('click', function() { taskDiv.remove(); }); } function highlightTask() { this.classList.add('highlighted'); } function unhighlightTask() { this.classList.remove('highlighted'); } function copyToClipboard(text) { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(text) .then(() => { alert('Задача скопирована в буфер обмена!'); }) .catch(err => { console.error('Не удалось скопировать текст: ', err); fallbackCopyToClipboard(text); }); } else { fallbackCopyToClipboard(text); } } function fallbackCopyToClipboard(text) { const textArea = document.createElement("textarea"); textArea.value = text; // Скрываем textarea textArea.style.position = "fixed"; textArea.style.top = 0; textArea.style.left = 0; textArea.style.width = "2em"; textArea.style.height = "2em"; textArea.style.padding = 0; textArea.style.border = "none"; textArea.style.outline = "none"; textArea.style.boxShadow = "none"; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { const successful = document.execCommand('copy'); const msg = successful ? 'успешно' : 'не удалось'; alert('Задача скопирована в буфер обмена ' + msg); } catch (err) { console.error('Не удалось скопировать текст: ', err); alert('Не удалось скопировать задачу в буфер обмена'); } document.body.removeChild(textArea); } });