简体   繁体   English

为什么我在Javascript中的功能不起作用?

[英]Why my function in Javascript is not working?

I am trying differents methods to implement a function, but one o them isn't working. 我正在尝试使用不同的方法来实现一个函数,但是其中一个方法不起作用。 Are this syntax correct?: 这种语法是否正确?:

function funcao2()
        {
            alert('Tudo bem?');
        }funcao2();

I have a 'self invoking function' , an 'anonymous function' and a 'function attributed to a variable' , but the second aren't working. 我有一个'自调用函数' ,一个'匿名函数'和一个'归因于变量的函数' ,但第二个函数不起作用。 See the code: 看代码:

<script type="text/javascript">
    //Função de auto-invocação anônima ou função recursiva anônima
    (function(){
        alert('Oi');
    })();
    //Função anônima
    document.onload = function(){
        alert('Página carregada');
    };
    //Atribuir função a uma variável e executá-la em seguida
    var funcao = function(){
        alert('Oi novamente');
    }; funcao();

Commented this, seems to be what the OP wanted to know, so posting it as an answer: 评论这个,似乎是OP想知道的,所以发布它作为答案:

document.onload should be window.onload , document has the onreadystatechange event, the window loads document.onload应该是window.onloaddocumentonreadystatechange事件, window加载

related: 有关:

when using the document.onreadystatechange event, check the status and readystate properties: 使用document.onreadystatechange事件时,请检查状态和readystate属性:

document.onreadystatechange = function(e)
{
    if (this.readyState === 4 && this.status === 200)
    {
        //only now, the document is loaded
        return;
    }
    //do stuff on readyState 1,2,3... <-- usefull when loading is likely to fail
}

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

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