开发者

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

开发者 https://www.devze.com 2022-12-18 00:59 出处:网络
I have the following jquer开发者_C百科y function $(function(){ $(\'.buttonEffectWrapper a\').not(\'#browse-all a\').hover(function(){

I have the following jquer开发者_C百科y function

$(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);
  })

} );

On mouseover, I copy the contents of #mainImageBrowser to variable imageBrowserContent It then does an Ajax function which replaces the html content of #mainImageBrowser and then alerts me of what the contents are. All of that works.

My intent was to then replace #mainImageBrowser with the original contents, which would be stored in imageBrowserContent. I couldn't get this to work, so I put alert(imageBrowserContent); and no alert every pops up. What am I missing? If I put a text string in the alert, it popups fine, so I am confused...


After reformatting, the problem shows. imageBrowserContent is a local variable of the first function. It will not be visible inside the second function. If you move the variable declaration to the parent function, it will work.

$(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);
    })
});
0

精彩评论

暂无评论...
验证码 换一张
取 消