我有以下 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);
})
});
关于javascript - 如何在两个单独的函数中使用 JavaScript 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2120291/