变量范围为 tabs() jQuery UI 函数

Variable scope into tabs() jQuery UI function

本文关键字:jQuery UI 函数 范围 tabs 变量      更新时间:2023-09-26
$(function() {
    $("#tabs").tabs({
        beforeActivate: function (event, ui) {
            var tab = "";
            tab = ui.newPanel.attr('id');
            if( $('#tab-inserimento').attr('id') === tab ){
                alert("a");
            }else if( $('#tab-ricerca').attr('id') === tab ){
                alert("b");
            }
        }
    });
    apriTab(tab);
});

我对这个功能有问题。 tab变量获取当前所选选项卡的 id 值,确定。

但是,我还需要将此变量用于apriTab()函数。但是,存在范围问题。就像代码是写的apriTab()看不到变量一样。为了解决这个问题,我可以在$("#tabs").tabs({..之外声明tab.这样我就会制造另一个问题:tab = ui.newPanel.attr('id');它不起作用。我确定,因为我使用 chrome 控制台进行了调试,并且整个功能未执行。

更新:

$(function() {
$("#tabs").tabs({
    beforeActivate: function (event, ui) { 
        var tab = "";
        tab = ui.newPanel.attr('id');
        if( $('#tab-inserimento').attr('id') === tab ){
            getNotifiche(LIV_NOTIFICA_TAB, AREA_TEMATICA_PAGAMENTI, TAB_GESTIONE);
        }else if( $('#tab-ricerca').attr('id') === tab ){
            getNotifiche(LIV_NOTIFICA_TAB, AREA_TEMATICA_PAGAMENTI, TAB_CONSULTA);
        }
        apriTab(tab);
    }
});
apriTab(tab); //Here, I get an error because tab variable is not visible. But, with this error, the code above of this call works! And, always about this call, if I remove it, nothing works again! How it's possible?!
});

或者你可能有两个不同的变量

$(function() {
    var tab = "";
    $("#tabs").tabs({
        beforeActivate: function (event, ui) {
            tab = ui.newPanel.attr('id');
            if( $('#tab-inserimento').attr('id') === tab ){
                alert("a");
            }else if( $('#tab-ricerca').attr('id') === tab ){
                alert("b");
            }
        }
    });
    apriTab(tab);
});