问题
On my calendar there are dates that have events. The dates with the events should be highlighted with data received from a JSON
call. When I click on an event date the event should be shown. The problem is my JSON
data is not being fetched and the events don't show. Also, the weekday isn't right.
My showInfo
code that makes the JSON
request is below. The Codepen shows the full code example.
// show info
function showInfo(event) {
// link
var url = 'https://codepen.io/nakome/pen/EWBMzm.css';
// get json
getjson(url, function(obj) {
for (var key in obj) {
// if has envent add class
if(_('[data-id="' + key + '"]')){
_('[data-id="' + key + '"]').classList.add('event');
}
if (event === key) {
_('#calendar_data').classList.toggle('show_data');
// template info
var data = '<a href="#" class="hideEvent" '+
'onclick="return hideEvent();">×</a>'+
'<h3>' + obj[key].type + '</h3>' +
'<dl>' +
'<dt><dfn>Title:</dfn></dt><dd>' + obj[key].title + '</dd>' +
'<dt><dfn>Hour:</dfn></dt><dd>' + obj[key].time + '</dd>' +
'<dt><dfn>Venue:</dfn></dt><dd>' + obj[key].venue + '</dd>' +
'<dt><dfn>Location:</dfn></dt><dd>' + obj[key].location + '</dd>' +
'<dt><dfn>Description:</dfn></dt><dd>' + obj[key].desc + '</dd>' +
'<dt><dfn>More Info:</dfn></dt><dd><a href="' + obj[key].more +
'" title="More info">Here</a></dd>' +
'</dl>';
return _('#calendar_data').innerHTML = data;
}
}
});
return false;
}
// toggle event show or hide
function hideEvent(){
_('#calendar_data').classList.toggle('show_data');
}
回答1:
I think the problem is that your showInfo() was commented out at the start of the calendar() function. Remove the comment so this:
// showInfo();
Becomes this:
showInfo();
I have updated the Codepen for the original question here so you can see that the events are showing up. I'm not sure what you meant by the 'weekday is not right'.
回答2:
I tried the following, it works partly, only the Sunday gets no other color, do not know why.
// Days of week
html += '<tr class="week_cal">';
for (i = 0; i < 7; i++) {
if (day -1 == i) {
html += '<th class="week_event">' + weekdays[i] + '</th>';
} else {
html += '<th>' + weekdays[i] + '</th>';
}
}
html += '</tr>';
html += '</thead>';
来源:https://stackoverflow.com/questions/44359639/json-event-does-not-work-and-the-weekday-is-not-right