首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >生成“事件”函数数组

生成“事件”函数数组
EN

Stack Overflow用户
提问于 2015-10-02 08:00:47
回答 1查看 37关注 0票数 0

我编写了我的第一个jquery插件(日历)。现在一切都很好,但是我已经把里面的事件嵌入到我的插件中,比如:

代码语言:javascript
运行
复制
var days = [
    new Event('9-5', 'test1', 1),
    new Event('9-7', 'test2', 8),
    new Event('9-8', 'test3', 2)       
];

现在,我将事件“天”从外部提交到我的插件中:

代码语言:javascript
运行
复制
var days = [
    ['9-5','test1', 1],
    ['9-7', 'test2', 8],
    ['9-8', 'test3', 2]      
];
$("#cal").calendar( { year: "2015", month: "9", events: days } ); 

在插件中

代码语言:javascript
运行
复制
var config = {
// default settings
    year: "2015",
    month: "9",
    events: ""
// ...
};
// change default settings
if (settings) { config = $.extend( {}, config, settings ); }

现在,我的问题是用以下方法动态地生成相同的事件数组:

代码语言:javascript
运行
复制
var days = [];
config.events.each(function( index ) {
    days.push(new Event(config.events[index][0],config.events[index][1],config.events[index][2]));
});

但我得到了以下错误:

代码语言:javascript
运行
复制
TypeError: config.events.each is not a function

以下是我的活动功能:

代码语言:javascript
运行
复制
function Event(start,title, duration){ 

    if(start instanceof Date){
        this.start = start;   
    }
    this.title = title;
    this.dur = duration;
    this.end= new Date(this.start);
    ....
}

怎么做才对?非常感谢

编辑

我用了每个功能都不对:

代码语言:javascript
运行
复制
 config.events.each(function( index ) {

必须:

代码语言:javascript
运行
复制
 $.each(config.events, function( index ) {
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-02 08:25:31

您没有正确地使用.each。$.each()采用两个参数。第一个是数组/对象,第二个是回调函数。http://api.jquery.com/jquery.each/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32902856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档