gpt4 book ai didi

javascript - 获取对象值并将其添加到 HTML Div

转载 作者:行者123 更新时间:2023-12-02 22:22:49 25 4
gpt4 key购买 nike

我有一个 taskObject,其中包含任务标题等值。

我想将该任务的标题添加到 HTML Div,但似乎找不到在 tasksManager.add.title 上获取标题以获取值的方法。前端结果是用户每次登录应用程序时都能看到他们在某一天正在执行哪些任务。

这是我的代码

//Task Manager//

var idCounter = 0
var tasksManager = {
array: [],
add: function(task, bracketStart, bracketEnd, date) {
taskObject = {
title: task,
idVerification: idCounter++,
startTask: bracketStart,
endTask: bracketEnd,
dateTask: date
}
tasksManager.array.push(taskObject)
},
show: function(id) {
var i;
for (i = 0; i < tasksManager.array.length; i++) {
if (id === tasksManager.array[i].idVerification) {
return tasksManager.array[i]
}
}
},
delete: function(task) {
if (this.show) {
tasksManager.array.splice(task)
}
}

}

// Calendar

var myDate = new Date(),
year = myDate.getFullYear(),
month = myDate.getMonth(),
day = myDate.getDay(),
date = myDate.getDate()

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var dates = ["0", "1st", "2nd", "3rd", "4th", "5th", "1st", "1st", "1st", "1st", "1st", "1st", "1st", "1st"]


var dayElement = document.createElement('div')
dayElement.innerHTML = days[day] + "," + " " + dates[date] + " " + "of" + " " + months[month] + " " + year
document.getElementById("month-today").appendChild(dayElement)

function removeClass() {
document.getElementById('task-input').classList.toggle('new-task');
}

var button = document.getElementById("save-task");
var input = document.getElementById("add-new-task");
var startTime = document.getElementById("time-stamp");
var endTime = document.getElementById("time-stamp-finish");
var date = document.getElementById("event-date")
// -- - THIS IS MY ATTEMPT TO ADD THE OBJECT TITLE TO DIV-- -
var taskEvent = document.createElement('div')
taskEvent.innerHTML = tasksManager.array.add.title
document.getElementById("event-1").appendChild(taskEvent)


button.addEventListener('click', () => {
tasksManager.add(input.value, startTime.value, endTime.value, date.value);
console.log(tasksManager.array)
})
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>ProdPlus 💪</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="css/style.css">
<link href="https://fonts.googleapis.com/css?family=Tomorrow&display=swap" rel="stylesheet">
</head>

<body>
<h1>ProdPlus+ 💪</h1>
<div class="dropdown-menu">

</div>
<div id="month-today"></div>
<div id="day-today"></div>
<div id="date-today"></div>


<button class="add-task" onclick="removeClass()">Add task</button>

<div class="new-task" id="task-input">
<div>Task: <input id="add-new-task" type="text"></div>
<div id="add-time">
<label>Time</label>
<select id="time-stamp">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
</select>
-
<select id="time-stamp-finish">
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
</select>
</div>
<div id="date-stamp">
Date:
<input type="date" id="event-date" name="bday" max="2030-12-31"><br>
</div>
<button class="save-task" id="save-task" onclick="">Save task</button>
</div>
<div class="calendar">
<!-- <div class="month-indicator">
<div>January</div>
<div>February</div>
<div>March</div>
<div>April</div>
<div>June</div>
<div>July</div>
<div>August</div>
<div>September</div>
<div>October</div>
<div>November</div>
<div>December</div>
</div> -->
<div class="day-of-week">
<div class="day">Sunday</div>
<div class="day">Monday</div>
<div class="day">Tuesday</div>
<div class="day">Wednesday</div>
<div class="day">Thursday</div>
<div class="day">Friday</div>
<div class="day">Saturday</div>
</div>

<div class="hours">
<div>04:00</div>
<div>05:00</div>
<div>06:00</div>
<div>07:00</div>
<div>08:00</div>
<div>09:00</div>
<div>10:00</div>
<div>11:00</div>
<div>12:00</div>
<div>13:00</div>
<div>14:00</div>
<div>15:00</div>
<div>16:00</div>
<div>17:00</div>
<div>18:00</div>
<div>19:00</div>
<div>20:00</div>
<div>21:00</div>
<div>22:00</div>
<div>23:00</div>
<div>00:00</div>
<div>01:00</div>
<div>02:00</div>
<div>03:00</div>

</div>
<div class="grid-container">
<div class="events">
<div class="event" id="event-1"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
</div>
</div>

<div id="dates">

</div>

<script src="script.js"></script>
</body>

</html>

最佳答案

创建任务事件后,您需要将附加移动到点击内部

//Task Manager//

var idCounter = 0
var tasksManager = {
array: [],
add: function(task, bracketStart, bracketEnd, date) {
taskObject = {
title: task,
idVerification: idCounter++,
startTask: bracketStart,
endTask: bracketEnd,
dateTask: date
}
tasksManager.array.push(taskObject)
},
show: function(id) {
var i;
for (i = 0; i < tasksManager.array.length; i++) {
if (id === tasksManager.array[i].idVerification) {
return tasksManager.array[i]
}
}
},
delete: function(task) {
if (this.show) {
tasksManager.array.splice(task)
}
}

}

// Calendar

var myDate = new Date(),
year = myDate.getFullYear(),
month = myDate.getMonth(),
day = myDate.getDay(),
date = myDate.getDate()

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var dates = ["0", "1st", "2nd", "3rd", "4th", "5th", "1st", "1st", "1st", "1st", "1st", "1st", "1st", "1st"]


var dayElement = document.createElement('div')
dayElement.innerHTML = days[day] + "," + " " + dates[date] + " " + "of" + " " + months[month] + " " + year
document.getElementById("month-today").appendChild(dayElement)

function removeClass() {
document.getElementById('task-input').classList.toggle('new-task');
}

var button = document.getElementById("save-task");
button.addEventListener('click', () => {
var input = document.getElementById("add-new-task");
var startTime = document.getElementById("time-stamp");
var endTime = document.getElementById("time-stamp-finish");
var date = document.getElementById("event-date")
var taskEvent = document.createElement('div')
tasksManager.add(input.value, startTime.value, endTime.value, date.value);
//console.log(tasksManager.array)
taskEvent.innerHTML = tasksManager.array[tasksManager.array.length - 1].title
document.getElementById("event-1").appendChild(taskEvent)
})
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>ProdPlus 💪</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="css/style.css">
<link href="https://fonts.googleapis.com/css?family=Tomorrow&display=swap" rel="stylesheet">
</head>

<body>
<h1>ProdPlus+ 💪</h1>
<div class="dropdown-menu">

</div>
<div id="month-today"></div>
<div id="day-today"></div>
<div id="date-today"></div>


<button class="add-task" onclick="removeClass()">Add task</button>

<div class="new-task" id="task-input">
<div>Task: <input id="add-new-task" type="text"></div>
<div id="add-time">
<label>Time</label>
<select id="time-stamp">
<option value="08:00">08:00</option>
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
</select>
-
<select id="time-stamp-finish">
<option value="09:00">09:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
</select>
</div>
<div id="date-stamp">
Date:
<input type="date" id="event-date" name="bday" max="2030-12-31"><br>
</div>
<button class="save-task" id="save-task" onclick="">Save task</button>
</div>
<div class="calendar">
<!-- <div class="month-indicator">
<div>January</div>
<div>February</div>
<div>March</div>
<div>April</div>
<div>June</div>
<div>July</div>
<div>August</div>
<div>September</div>
<div>October</div>
<div>November</div>
<div>December</div>
</div> -->
<div class="day-of-week">
<div class="day">Sunday</div>
<div class="day">Monday</div>
<div class="day">Tuesday</div>
<div class="day">Wednesday</div>
<div class="day">Thursday</div>
<div class="day">Friday</div>
<div class="day">Saturday</div>
</div>

<div class="hours">
<div>04:00</div>
<div>05:00</div>
<div>06:00</div>
<div>07:00</div>
<div>08:00</div>
<div>09:00</div>
<div>10:00</div>
<div>11:00</div>
<div>12:00</div>
<div>13:00</div>
<div>14:00</div>
<div>15:00</div>
<div>16:00</div>
<div>17:00</div>
<div>18:00</div>
<div>19:00</div>
<div>20:00</div>
<div>21:00</div>
<div>22:00</div>
<div>23:00</div>
<div>00:00</div>
<div>01:00</div>
<div>02:00</div>
<div>03:00</div>

</div>
<div class="grid-container">
<div class="events">
<div class="event" id="event-1"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
</div>
</div>

<div id="dates">

</div>

<script src="script.js"></script>
</body>

</html>

关于javascript - 获取对象值并将其添加到 HTML Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59171268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com