2016-11-08 97 views
-1

我剛剛放棄了,試圖找到一整天的解決方案。幫助我理解這件事。爲什麼全局變量沒有更新?使用jQuery.tabs更新全局變量

我有這段代碼,它檢查標籤是否處於活動狀態,然後我想根據選擇哪個標籤更新變量。所以標籤切換工作正常,激活裏面的console.log給出了24,在外面沒有定義。

var last; 
jQuery('#tabs').tabs({ 
    active: 0, 
    activate: function(event, ui) { 
    if (ui.newPanel.is("#tabs-1")) { 
     var last = 24; 
     console.log(last) 
    } else if (ui.newPanel.is("#tabs-2")) { 
     var last = 48; 
    } else if (ui.newPanel.is("#tabs-3")) { 
     var last = 72; 
    }; 

    } 
}); 

console.log(last); 

我在這裏錯過了什麼?

+0

除了'last'是本地的'activate'功能,認爲這是執行功能時約。在執行'jQuery('#tabs')。tabs(...)''時,在頁面加載時執行* once *的同時,將在該示例結尾執行'console.log(last)'調用。只要單擊一個選項卡,'activate'函數就會在* load *之後* *後面調用。 –

回答

1

聲明一個var是函數作用域。請參閱What is the scope of variables in JavaScript?

您有一個var last在您的頁面頂部聲明是全局作用域。

您在activate函數中聲明瞭一個新變量var last = 24。這是一個不同的範圍,因爲它在一個函數內,所以它將成爲這個激活函數的局部變量。隨後的var last聲明被忽略,因爲已經存在於函數作用域中,但值將被分配給本地函數var last

您的console.log(last)範圍爲全球last,您從未給出任何價值。

如果您要使用全球last,請從您的函數中刪除聲明。編譯器將不會在該函數中找到本地的var last,並且將冒險到它將使用它的全局last的外部範圍。

var last; // global scope 
jQuery('#tabs').tabs({ 
    active: 0, 
    activate: function (event, ui) { 
    if (ui.newPanel.is("#tabs-1")) { 
     last = 24; // remove var declaration to use global variable 
     console.log(last); 
    } 

    else if (ui.newPanel.is("#tabs-2")) { 
     last = 48; 
    } 

    else if (ui.newPanel.is("#tabs-3")) { 
     last = 72; 
    }; 

    } 
}); 

console.log(last); // global scope last 

var last = -1; // global scope 
 
var whereAmI = "GLOBAL SCOPE"; 
 

 
$('button').on('click',function() { // new scope within function 
 
    if (new Date().getMilliseconds() % 2 === 0) { 
 
     var whereAmI = "FUNCTION SCOPE"; 
 
     var last = 24; 
 
     console.log(last); 
 
     console.log(whereAmI); 
 
    } else { 
 
     last = 48; 
 
     whereAmI = "FUNCTION SCOPE"; 
 
     console.log(last); 
 
     console.log(whereAmI); 
 
    } 
 
}); 
 

 
console.log(last); // global scope 
 
console.log(whereAmI); // global scope
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button>Click Me!</button>

0

activate處理程序使用var使得一個局部變量因此不更新所述外部範圍可變

var last; 
 
jQuery('#tabs').tabs({ 
 
    active: 0, 
 
    activate: function(event, ui) { 
 
    if (ui.newPanel.is("#tabs-1")) { 
 
     last = 24; 
 
     console.log(last) 
 
    } else if (ui.newPanel.is("#tabs-2")) { 
 
     last = 48; 
 
    } else if (ui.newPanel.is("#tabs-3")) { 
 
     last = 72; 
 
    }; 
 

 
    } 
 
}); 
 

 
$('button').click(function() { 
 
    $('#inputlast').val(last); 
 
})
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" /> 
 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
 
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
<div id="tabs"> 
 
    <ul> 
 
    <li><a href="#tabs-1">Nunc tincidunt</a> 
 
    </li> 
 
    <li><a href="#tabs-2">Proin dolor</a> 
 
    </li> 
 
    <li><a href="#tabs-3">Aenean lacinia</a> 
 
    </li> 
 
    </ul> 
 
    <div id="tabs-1"> 
 
    <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper 
 
     leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. 
 
     Nunc tristique tempus lectus.</p> 
 
    </div> 
 
    <div id="tabs-2"> 
 
    <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean 
 
     aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. 
 
     Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p> 
 
    </div> 
 
    <div id="tabs-3"> 
 
    <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia 
 
     nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p> 
 
    <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna 
 
     ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. 
 
     Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p> 
 
    </div> 
 
</div> 
 

 
<br /> 
 
<input id="inputlast" readonly /> 
 
<button>Value</button>