无法在函数内部获取全局变量(javascript) [英] Can't get global variables inside the function (javascript)

查看:126
本文介绍了无法在函数内部获取全局变量(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,但没有任何帮助。如果您有任何想法,请帮助我,这是非常重要的项目。

How can I get global variables "selection" and "closed" to use them. I tried "window.selection" and "window.closed", but nothing helps. If you have an idea, help me please, it's so important project.

推荐答案

全球已关闭变量 only:这是窗口 s .closed 属性 - 例如之前发生了 .name : - )

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

使用IEFE使您的变量成为本地:

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.

这篇关于无法在函数内部获取全局变量(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆