将变量从函数传递到fancybox函数

时间:2012-02-12 13:15:35

标签: jquery variables fancybox

我在函数中创建了一个变量,我需要将它传递到fancybox onCleanup函数,以便在父页面中进一步处理,但on onCleanup函数无法获取变量。这就是我现在所拥有的......它可以看到'var fontSize ='sdfs'“但不是在点击功能中创建的变量。

//Customise font size
$('#font-sizer a').on('click',function(){
    $('body').removeClass('font-size-small font-size-medium font-size-large font-size-very-large');
    $('#font-sizer a').removeClass('selected');
    switch($(this).attr("class"))
    {
    case "font-small":
        fontSize = "font-small";
        break;
    case "font-medium":
        fontSize = "font-medium";
        break;
    case "font-large":
        fontSize = "font-large";
        break;
    case "font-very-large":
        fontSize = "font-very-large";
        break;
    }
    $(this).addClass('selected');
    return false;
});
var fontSize = "sdfs";


    //Customise and Add Widgets Lightbox
    $("#customise,#add-widgets,#launch-profile,#view-all-apps").fancybox({
        'padding' : 0,
        'width'             : 940,
        'height'            : 634,
        'autoScale'         : false,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'type'              : 'iframe',
        'scrolling'     : 'no',
        'onCleanup' : function() {
            window.parent.ifDoneChildFrame(fontSize);
        }
    });

1 个答案:

答案 0 :(得分:1)

如果您想要使用全局变量,那么您需要做的就是放置:

var fontSize;

...在脚本的顶部,就在里面:

$(document).ready(function(){

您必须删除其他声明:

var fontSize = "sdfs";

通过在特定函数之外声明fontSize变量,即使没有给它赋值,它也可以被所有函数访问。当然,如果变量需要通过不同的函数以不同的方式使用,这可能会有问题。

有关变量范围的更多信息,请访问:http://www.w3schools.com/js/js_variables.asp