我试图“全局化”我的变量,以便在多个函数中使用它们但没有结果。我哪里错了? 非常感谢你!
以下是代码:
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;
});
}
答案 0 :(得分:1)
粗略猜测,将变量初始化放在文档准备好后执行的块中会解决问题。
尝试使用此代替前两行:
$(function() {
window.my_sidebar_clicked = $('#dash #sidebar ul#main_menu li');
window.my_dash_main_content = $('#dash > #main_content');
});