您可以使用HTMLElement.dataset讀/寫自定義數據屬性。一個example app is HERE。您也可以直接使用localStorage,而唯一的ID將會是日期。
爲了在日曆單元格中顯示文本/數據,您需要一個佔位符。在我的例子中,每個單元格的格式如下:
<td>
<div data-date="6-1-2015" class="box">
<span class="day">6</span>
<span class="text"></span> <!-- this is a placeholder -->
</div>
</td>
這種方式很容易找到可點擊的細胞:
// Select clickable cells
var cells = document.querySelectorAll('.box');
比綁定click事件(打開模態窗口):
// Bind clickable cells
for (var i = 0; i < cells.length; i++) {
cells[i].addEventListener('click', function(e) {
var thisCell = this;
var thisDate = thisCell.dataset.date;
var thisEvent = thisCell.dataset.event;
var thisParticipants = thisCell.dataset.participants;
var thisDescription = thisCell.dataset.description;
date.innerHTML = thisDate;
event.value = thisEvent || '';
participants.value = thisParticipants || '';
description.value = thisDescription || '';
modal.style.display = 'block';
});
};
保存數據是一塊蛋糕:
// Bind save button
submit.addEventListener('click', function(e) {
var thisDate = date.innerHTML;
var thisEvent = event.value;
var thisParticipants = participants.value;
var thisDescription = description.value;
var thisCell = document.querySelector('[data-date="' + thisDate + '"]');
// Assign data to table cell
thisCell.dataset.event = thisEvent;
thisCell.dataset.participants = thisParticipants;
thisCell.dataset.description = thisDescription;
// Show on screen
var textField = thisCell.querySelector('.text'); //-- This is our container
textField.innerHTML = '\
<span class="eve">' + thisEvent + '</span>\
<span class="par">' + thisParticipants + '</span>\
<span class="des">' + thisDescription + '</span>\
';
modal.style.display = 'none';
});
和填充數據後,表格單元格看起來是這樣的:
<td>
<div data-date="6-1-2015" class="box" data-event="Event Title" data-participants="John, Robert" data-description="Football">
<span class="day">6</span>
<span class="text">
<span class="eve">Event Title</span>
<span class="par">John, Robert</span>
<span class="des">Football</span>
</span>
</div>
</td>
欲瞭解更多信息,請看看小提琴,代碼是巨大的,以粘貼到此處。
爲什麼在這個世界上,如果它明確地表示**而不是打算用角來完成,那麼爲什麼在這個世界上你會用「角」來標記一個問題?刪除標籤 – 2015-01-26 21:35:11
我不明白,這裏的實際問題是什麼? – 2015-01-26 21:36:31
'.innerHTML'不是您要查找的屬性。 '.value'就是你需要的。 – giuscri 2015-01-26 21:38:53