简体   繁体   English

全局变量,Jquery

[英]Global variables, Jquery

I have the following code我有以下代码

oss = $("#us");
mainLogo = $("#mainLogo");
container = $(".container");

$(document).ready(function(){
    oss.hide();
    oss.fadeIn(1000);
    mainLogo.hide();
    mainLogo.fadeIn(1000);
    container.find("#images").hide();
    container.find("#images").slideDown();
})
$(oss).click(function(){
    container.fadeOut();
})

The problem I have is that I can't seem to access the variables outside.我的问题是我似乎无法访问外部变量。 I've tried window.varName, just writing var infront etc. But the compiler seems to just skip it and thus not do anything.我试过 window.varName,只是写 var infront 等。但编译器似乎只是跳过它,因此什么也不做。

The following piece of code does the same you are trying to accomplish.以下代码与您尝试完成的操作相同。 This way you declare the global variables: oss, mainLogo and container outside the scope of document.ready().通过这种方式,您可以在 document.ready() 范围之外声明全局变量:oss、mainLogo 和 container。

var oss;
var mainLogo;
var container;

$(document).ready(function(){

    oss = $("#us");
    mainLogo = $("#mainLogo");
    container = $(".container");

    oss.hide();
    oss.fadeIn(1000);
    mainLogo.hide();
    mainLogo.fadeIn(1000);
    container.find("#images").hide();
    container.find("#images").slideDown();


    oss.click(function(){
        container.fadeOut();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM