多次使用同一个 jQuery 函数
我正在尝试一些新的东西,并在我学会爬行之前尝试跑步。我在测试页面中使用 jQuery 脚本来检索和显示来自 google 日历的事件。该脚本工作得很好(因为它是由其他人制作的!)
我正在使用该脚本来过滤掉仅包含特定文本的事件,所有这些都显示在 div
内。
然后,我想在另一个 div
中使用不同的过滤器显示一组不同的结果,我只是想我可以再次调用该函数,但是当我这样做时,结果并不总是最终出现在正确的 div
并且有时会添加到同一个或上一个框中。每次刷新都会得到不同的结果。
结果是正确的,因此提要正在工作,因此我认为这与文档未准备好或提要延迟有关?
谁能指出我正确的方向吗?
页面中的代码(匿名馈送):
jQuery(function ($) {
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'ladies',
targetDiv:'#ladiesEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'seniors',
targetDiv:'#seniorEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'open',
targetDiv:'#openEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'social',
targetDiv:'#socialEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'junior',
targetDiv:'#juniorEvents'
});
});
这是被调用的函数:
(function ($) {
//Add gcal element
$(document).ready(function () {
$('body').prepend('<div id="loading">Loading...</div>');
});
//Resize image on ready or resize
$.gCalReader = function (options) {
//Default settings
var settings = {
//defaults??
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 20,
displayCount: 1
};
var feedUri = options.feedUri;
if (feedUri.indexOf("public/full") == -1) {
feedUri = settings.feedUri;
}
var options = $.extend(settings, options);
//properties form options are combined with settings??
function _run() {
var calendarService = new google.gdata.calendar.CalendarService('GoogleInc-jsguide-1.0');
// The "public/full" feed is used to retrieve events from the named public calendar with full projection.
var query = new google.gdata.calendar.CalendarEventQuery(feedUri);
// Set the query with the query text
query.setFullTextQuery(options.textfilter);
query.setOrderBy('starttime');
query.setSortOrder('ascending');
query.setFutureEvents(true);
query.setSingleEvents(true);
query.setMaxResults(options.maxresults);
//alert(options.targetDiv);
var callback = function (result) {
var entries = result.feed.getEntries();
//clear the loading div
$('#loading').html('');
if (options.displayCount) {
$(options.targetDiv).append(entries.length + ' upcoming events');
}
$(options.targetDiv).append('<ul id="eventlist"></ul>');
for (var i = 0; i < entries.length; i++) {
var eventEntry = entries[i];
var eventTitle = eventEntry.getTitle().getText();
var startDateTime = null;
var eventDate = null;
var eventWhere = null;
var eventContent = eventEntry.getContent().getText();
var times = eventEntry.getTimes();
if (times.length > 0) {
startDateTime = times[0].getStartTime();
eventDate = startDateTime.getDate();
}
var d_names = new Array("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat");
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
var a_p = "";
var d = eventDate;
var curr_hour = d.getHours();
if (curr_hour < 12) {
a_p = "am";
}
else {
a_p = "pm";
}
if (curr_hour == 0) {
curr_hour = 12;
}
if (curr_hour > 12) {
curr_hour = curr_hour - 12;
}
var curr_min = d.getMinutes();
curr_min = curr_min + "";
if (curr_min.length == 1) {
curr_min = "0" + curr_min;
}
var time = curr_hour + ':' + curr_min + a_p;
var day = eventDate.getDay();
var month = eventDate.getMonth();
var date = eventDate.getDate();
var dayname = d_names[day];
var monthname = m_names[month];
var location = eventEntry.getLocations();
var eventWhere = location[0].getValueString();
var eventhtml = '<div id="eventtitle">' + eventTitle + '</div> When: ' + dayname + ' ' + monthname + ' ' + date + ', ' + time + '<br>Where: ' + eventWhere + '<br>' + eventContent;
$('#eventlist').append('<li>' + eventhtml + '</li>');
}
};
// Error handler to be invoked when getEventsFeed() produces an error
var handleError = function (error) {
$('#gcal').html('<pre>' + error + '</pre>');
};
// Submit the request using the calendar service object
calendarService.getEventsFeed(query, callback, handleError);
}
google.setOnLoadCallback(_run);
$(window).load(function () {
}); //End window load
};
})(jQuery);
谢谢
更新:因为我不能回答,直到再过 7 小时... 哈哈!!!!
发现问题了——这一切都归咎于事件列表元素的ID。
因为页面中现在有该元素的多个实例,所以我假设它正在将结果转储到任何可用的结果中。
我已经对其进行了编码,以使每个事件列表的 id 都是唯一的,现在它可以
正常工作了!
I'm trying something new and trying to run before I can crawl. I'm using a jQuery script in a test page that retrieves and displays events from a google calendar. The script works very well (because it was made by someone else!!)
I'm using the script to filter out events that only include a certain text, again all working and these are displayed inside a div
.
I then want to display a different set of results with a different filter in another div
and I simply thought I could just call the function again, however when I do this the results do not always end up in the correct div
and sometimes get added to the same or previous box. Each refresh gets different results.
The results are correct so the feed is working so I assume it's something to do with document not being ready or perhaps delay in the feed?
Can anyone point me in the right direction please.
Code in page (feed anonymised):
jQuery(function ($) {
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'ladies',
targetDiv:'#ladiesEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'seniors',
targetDiv:'#seniorEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'open',
targetDiv:'#openEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'social',
targetDiv:'#socialEvents'
});
$.gCalReader({
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 4,
textfilter: 'junior',
targetDiv:'#juniorEvents'
});
});
And here's the function being called:
(function ($) {
//Add gcal element
$(document).ready(function () {
$('body').prepend('<div id="loading">Loading...</div>');
});
//Resize image on ready or resize
$.gCalReader = function (options) {
//Default settings
var settings = {
//defaults??
feedUri: 'https://www.google.com/calendar/feeds/[email protected]/public/full',
maxresults: 20,
displayCount: 1
};
var feedUri = options.feedUri;
if (feedUri.indexOf("public/full") == -1) {
feedUri = settings.feedUri;
}
var options = $.extend(settings, options);
//properties form options are combined with settings??
function _run() {
var calendarService = new google.gdata.calendar.CalendarService('GoogleInc-jsguide-1.0');
// The "public/full" feed is used to retrieve events from the named public calendar with full projection.
var query = new google.gdata.calendar.CalendarEventQuery(feedUri);
// Set the query with the query text
query.setFullTextQuery(options.textfilter);
query.setOrderBy('starttime');
query.setSortOrder('ascending');
query.setFutureEvents(true);
query.setSingleEvents(true);
query.setMaxResults(options.maxresults);
//alert(options.targetDiv);
var callback = function (result) {
var entries = result.feed.getEntries();
//clear the loading div
$('#loading').html('');
if (options.displayCount) {
$(options.targetDiv).append(entries.length + ' upcoming events');
}
$(options.targetDiv).append('<ul id="eventlist"></ul>');
for (var i = 0; i < entries.length; i++) {
var eventEntry = entries[i];
var eventTitle = eventEntry.getTitle().getText();
var startDateTime = null;
var eventDate = null;
var eventWhere = null;
var eventContent = eventEntry.getContent().getText();
var times = eventEntry.getTimes();
if (times.length > 0) {
startDateTime = times[0].getStartTime();
eventDate = startDateTime.getDate();
}
var d_names = new Array("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat");
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
var a_p = "";
var d = eventDate;
var curr_hour = d.getHours();
if (curr_hour < 12) {
a_p = "am";
}
else {
a_p = "pm";
}
if (curr_hour == 0) {
curr_hour = 12;
}
if (curr_hour > 12) {
curr_hour = curr_hour - 12;
}
var curr_min = d.getMinutes();
curr_min = curr_min + "";
if (curr_min.length == 1) {
curr_min = "0" + curr_min;
}
var time = curr_hour + ':' + curr_min + a_p;
var day = eventDate.getDay();
var month = eventDate.getMonth();
var date = eventDate.getDate();
var dayname = d_names[day];
var monthname = m_names[month];
var location = eventEntry.getLocations();
var eventWhere = location[0].getValueString();
var eventhtml = '<div id="eventtitle">' + eventTitle + '</div> When: ' + dayname + ' ' + monthname + ' ' + date + ', ' + time + '<br>Where: ' + eventWhere + '<br>' + eventContent;
$('#eventlist').append('<li>' + eventhtml + '</li>');
}
};
// Error handler to be invoked when getEventsFeed() produces an error
var handleError = function (error) {
$('#gcal').html('<pre>' + error + '</pre>');
};
// Submit the request using the calendar service object
calendarService.getEventsFeed(query, callback, handleError);
}
google.setOnLoadCallback(_run);
$(window).load(function () {
}); //End window load
};
})(jQuery);
Thanks
UPDATE: cos I can;t answer until another 7 hours...
Ha ha!!!!
Found the problem - it was all down to the ID of the event list element.
Because there were now multiple instances of the element within the page I assume it was dumping the results in any that was available.
I've coded it to make the id of each event list unique and now it works
Eureka!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了——这一切都归咎于事件列表元素的ID。
因为页面中现在有该元素的多个实例,所以我假设它正在将结果转储到任何可用的结果中。
我已经对其进行了编码,以使每个事件列表的 id 都是唯一的,现在它可以工作了
Found the problem - it was all down to the ID of the event list element.
Because there were now multiple instances of the element within the page I assume it was dumping the results in any that was available.
I've coded it to make the id of each event list unique and now it works