我正试图"全球化"我的变量在多个函数中使用它们,但没有结果。我哪里错了?非常感谢!
代码如下:
var my_sidebar_clicked = $('#dash #sidebar ul#main_menu li');
var my_dash_main_content = $('#dash > #main_content');
function mainmenu_selection() {
//var my_sidebar_clicked = $('#dash #sidebar ul#main_menu li');
//var my_dash_main_content = $('#dash #main_content');
my_sidebar_clicked.children().click(function() {
var el = $(this);
var mylink = el.attr('href');
$('#dash #sidebar ul#main_menu li a').removeClass('current');
$(this).addClass('current');
//$('#dash #main_content').load("ajax/" + mylink);
my_dash_main_content.html('<img class="loader_img" src="common/img/ajax-loader.gif" />');
my_dash_main_content + $(".loader_img").fadeIn(200);
my_dash_main_content.load("ajax/" + mylink, function(response, status, xhr) {
if (status == "error") {
var msg = "Oops... sembra esserci un problema, ti preghiamo di riprovare ";
my_dash_main_content.html("<h4 class='error_msg'>" + msg + "</h4>"/* + xhr.status + " " + xhr.statusText*/);
}
});
return false;
});
}
大胆猜测,将变量初始化放在文档准备好后执行的块中可以解决问题。
试着这样来代替前两行:
$(function() {
window.my_sidebar_clicked = $('#dash #sidebar ul#main_menu li');
window.my_dash_main_content = $('#dash > #main_content');
});