簡體   English   中英

如何在兩個單獨的函數中使用JavaScript變量?

[英]How can I make use of a JavaScript variable across two separate functions?

我有以下jQuery函數

$(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); 而且沒有任何警報彈出。 我想念什么? 如果我在警報中放置一個文本字符串,它會很好地彈出,所以我很困惑...

重新格式化后,問題出現。 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);
    })
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM