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

后端 未结 1 1448
闹比i
闹比i 2021-01-28 18:46
var selection = document.getElementById(\'selection\');
var closed = true;

function openorclosebar() {

    if(closed == false){
        selection.style.webkitAnimation         


        
1条回答
  •  情话喂你
    2021-01-28 19:10

    The global closed variable is read-only: It's the windows .closed property - such has happened before with .name :-)

    Use an IEFE to make your variable local:

    (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;
            }
        }
    }());
    

    Also have a look at other unsafe names in browser environments.

    0 讨论(0)
提交回复
热议问题