簡體   English   中英

無法在函數內獲取全局變量(javascript)

[英]Can't get global variables inside the function (javascript)

var selection = document.getElementById('selection');
var closed = true;

function openorclosebar() {

    if(closed == false){
        selection.style.webkitAnimation='bounceOutDown 1s forwards';
        selection.style.animation='bounceOutDown 1s forwards';
        closed = false;
    }
    else{
        selection.style.webkitAnimation='bounceInUp 1s forwards';
        selection.style.animation='bounceInUp 1s forwards';
        closed = true;
    };
}

如何獲取全局變量“選擇”和“封閉”以使用它們。 我嘗試了“ window.selection”和“ window.closed”,但沒有任何幫助。 如果您有想法,請幫助我,這是非常重要的項目。

全局closed變量是只讀的:這是window.closed屬性- 之前已經發生過.name :-)

使用IEFE將變量設為本地:

(function() {
    var selection = document.getElementById('selection');
    var closed = true;

    function openorclosebar() {
        if(!closed) {
            selection.style.webkitAnimation='bounceOutDown 1s forwards';
            selection.style.animation='bounceOutDown 1s forwards';
            closed = false;
        } else {
            selection.style.webkitAnimation='bounceInUp 1s forwards';
            selection.style.animation='bounceInUp 1s forwards';
            closed = true;
        }
    }
}());

還可以查看瀏覽器環境中的其他不安全名稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM