2008-12-23 78 views
-2
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

問題是在showCal函數中myCal變量未定義。 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