生成 "Event" 函数数组
Generate "Event" function array
我写了我的第一个 jquery 插件(日历)。现在一切正常,但我已经将事件嵌入到我的插件中,例如:
var days = [
new Event('9-5', 'test1', 1),
new Event('9-7', 'test2', 8),
new Event('9-8', 'test3', 2)
];
现在我从外部将事件 "days" 提交到我的插件:
var days = [
['9-5','test1', 1],
['9-7', 'test2', 8],
['9-8', 'test3', 2]
];
$("#cal").calendar( { year: "2015", month: "9", events: days } );
在插件中
var config = {
// default settings
year: "2015",
month: "9",
events: ""
// ...
};
// change default settings
if (settings) { config = $.extend( {}, config, settings ); }
现在我的问题是我尝试动态生成相同的事件数组:
var days = [];
config.events.each(function( index ) {
days.push(new Event(config.events[index][0],config.events[index][1],config.events[index][2]));
});
但我收到以下错误:
TypeError: config.events.each is not a function
这是我的事件函数:
function Event(start,title, duration){
if(start instanceof Date){
this.start = start;
}
this.title = title;
this.dur = duration;
this.end= new Date(this.start);
....
}
怎么做才对?
非常感谢
编辑
我用过的各个功能都不对:
config.events.each(function( index ) {
必须是:
$.each(config.events, function( index ) {
您没有正确使用 .each。 $.each() 接受两个参数。第一个是 array/object,第二个是回调函数。 http://api.jquery.com/jquery.each/
我写了我的第一个 jquery 插件(日历)。现在一切正常,但我已经将事件嵌入到我的插件中,例如:
var days = [
new Event('9-5', 'test1', 1),
new Event('9-7', 'test2', 8),
new Event('9-8', 'test3', 2)
];
现在我从外部将事件 "days" 提交到我的插件:
var days = [
['9-5','test1', 1],
['9-7', 'test2', 8],
['9-8', 'test3', 2]
];
$("#cal").calendar( { year: "2015", month: "9", events: days } );
在插件中
var config = {
// default settings
year: "2015",
month: "9",
events: ""
// ...
};
// change default settings
if (settings) { config = $.extend( {}, config, settings ); }
现在我的问题是我尝试动态生成相同的事件数组:
var days = [];
config.events.each(function( index ) {
days.push(new Event(config.events[index][0],config.events[index][1],config.events[index][2]));
});
但我收到以下错误:
TypeError: config.events.each is not a function
这是我的事件函数:
function Event(start,title, duration){
if(start instanceof Date){
this.start = start;
}
this.title = title;
this.dur = duration;
this.end= new Date(this.start);
....
}
怎么做才对? 非常感谢
编辑
我用过的各个功能都不对:
config.events.each(function( index ) {
必须是:
$.each(config.events, function( index ) {
您没有正确使用 .each。 $.each() 接受两个参数。第一个是 array/object,第二个是回调函数。 http://api.jquery.com/jquery.each/