2010-01-22 59 views
0

我有以下的jQuery函數如何跨兩個獨立的函數使用JavaScript變量?

$(function(){ 

    $('.buttonEffectWrapper a').not('#browse-all a').hover(function(){ 

    pauseResume(); 
    var imageBrowserContent = $('#mainImageBrowser').html(); 
    $('#mainImageBrowserTabButtonWrapper, #slideshow > div') 
     .animate({opacity:"0"}, {duration:250}); 

    ajaxFunction('footer'); 
    alert(imageBrowserContent); 

    }, 
    function(){ 
    alert(imageBrowserContent); 
    }) 

}); 

鼠標懸停,我的#mainImageBrowser內容複製到變量imageBrowserContent 然後它做一個Ajax功能,它取代的#mainImageBrowser HTML內容,然後通知我的內容是什麼。所有這些工作。

我的意圖是用原始內容替換#mainImageBrowser,原始內容將存儲在imageBrowserContent中。我無法得到這個工作,所以我把alert(imageBrowserContent);,並沒有提醒每一個彈出。我錯過了什麼?如果我把一個文本字符串中的警報,它彈出良好,所以我很困惑...

回答

3

重新格式化後,問題顯示。 imageBrowserContent是第一個函數的局部變量。它在第二個函數中不可見。如果您將變量聲明移到父函數,它將起作用。

$(function(){ 
    var imageBrowserContent; 
    $('.buttonEffectWrapper a').not('#browse-all a').hover(function(){ 
     pauseResume(); 
     imageBrowserContent = $('#mainImageBrowser').html(); 

     $('#mainImageBrowserTabButtonWrapper, #slideshow > div') 
      .animate({opacity:"0"}, {duration:250}); 
     ajaxFunction('footer'); 
     alert(imageBrowserContent); 
    }, 
    function(){ 
     alert(imageBrowserContent); 
    }) 
}); 
相關問題