简体   繁体   English

如何调用另一个函数的 if 语句中的 function

[英]How to call a function which is inside another function's if statement

I am trying to run this type of code:我正在尝试运行这种类型的代码:

Javascript Code: Javascript 代码:

var a = new XMLHttpRequest();
a.open('GET', 'text.txt');
a.onreadystatechange = function() {
    var a_text = a.responseText;
    If (a_text == "text I have written."){
        document.getElementById("elem").innerHTML= a_text;
        function z() {
            document.getElementById("elem2").setAttribute("class", "invisible");
        };
    } else {
        alert("Sorry, Text doesn't match.");
    };
};
  
function click() {
    z(); 
    //Some other codes 
}

HTML Code: HTML 代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello</title>
        <style type="text/css" media="all">
            .invisible {
                display: none;
            }
        </style>
    </head>
    <body>
        <p id="elem"></p>
        <p id="elem2">Click the button to make me invisible.</p>
        <button onclick="click()">Click Me</button>
        <script src="javascript.js" type="text/javascript"></script>
    </body>
</html>

At the last I want that when I click on the button, function click() runs and function z() runs only if a_text matches required text.最后,我希望当我单击按钮时, function click()运行并且function z()仅在a_text匹配所需文本时运行。 But even if the text matches, the console shows that z() is not defined .但即使文本匹配,控制台也会显示z() is not defined

Please include if you have any alternative code to do it easily.如果您有任何替代代码可以轻松完成,请提供。

Help me out please.请帮帮我。

This is a part of my code where I need the help.这是我需要帮助的代码的一部分。 Solving this part will let me complete my project.解决这部分将使我完成我的项目。

z() is not defined. z() 未定义。

Because z() declared on inside the ajax callback not as a global.因为在 ajax 回调中声明的z()不是全局的。

And also not necessary do like this.而且也没有必要这样做。 You could do with declare as global function.您可以声明为全局 function。 And if you need any special behaviour with in function you could pass with params如果您需要 function 中的任何特殊行为,您可以使用参数传递

Updated更新

var a_text = '';

function z() {
  if(a_text == "text I have written.") {
    document.getElementById("elem").innerHTML = a_text;
    document.getElementById("elem2").setAttribute("class", "invisible");
  } else {
    alert("Sorry, Text doesn't match.");
  }
};



var a = new XMLHttpRequest();
a.open('GET', 'text.txt');
a.onreadystatechange = function() {
  a_text = a.responseText;
}

function click() {
  z();
  //Some other codes 
}

First you need to put z() declaration on the top level so that it can be called from click() .首先,您需要将z()声明放在顶层,以便可以从click()调用它。

Secondly, you have to create a flag shouldRunZ that will control whether z should run.其次,您必须创建一个标志shouldRunZ来控制z是否应该运行。

 function z() { document.getElementById("elem2").setAttribute("class", "invisible"); }; var shouldRunZ = false; var a = new XMLHttpRequest(); a.open('GET', 'text.txt'); a.onreadystatechange = function() { var a_text = a.responseText; if (a_text == "text I have written.") { shouldRunZ = true; document.getElementById("elem").innerHTML = a_text; } else { shouldRunZ = false; alert("Sorry, Text doesn't match."); }; }; function click() { shuldRunZ && z(); //Some other codes }

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

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