无法实现侦听器

时间:2008-12-23 09:08:13

标签: javascript

YAHOO.namespace("yuibook.calendar"); 

//define the lauchCal function which creates the calendar 
YAHOO.yuibook.calendar.launchCal = function() { 

    //create the calendar object, specifying the container 
    var myCal = new YAHOO.widget.Calendar("mycal"); 

    //draw the calendar on screen 
    myCal.render(); 
}

//define the showCal function which shows the calendar
Var showCal = function() { 
    //show the calendar 
    myCal.show(); 
} 

//create calendar on page load
YAHOO.util.Event.onDOMReady(YAHOO.yuibook.calendar.launchCal); 

//attach listener for click event on calendar icon 
YAHOO.util.Event.addListener("calico", "click", showCal); 

//myCal.hide();

代码的问题在于,当我点击图标时,我无法显示日历。任何人都可以帮忙。我知道这个听众

1 个答案:

答案 0 :(得分:1)

问题是myCal函数中showCal变量未定义。 myCal变量在launchCal函数中定义,只能在该函数的范围内使用。您需要存储对myCal变量的引用。

以下内容应该可以解决问题:

YAHOO.namespace("yuibook.calendar");

YAHOO.util.Event.onDOMReady(function() {
    YAHOO.yuibook.calendar.myCal = new YAHOO.widget.Calendar("mycal");
    YAHOO.yuibook.calendar.myCal.render();

    YAHOO.util.Event.addListener("calico", "click", YAHOO.yuibook.calendar.myCal.show); 
});

有关详细信息,请查看YUI文档中的this Popup Calendar example