简体   繁体   English

函数未在Javascript中运行

[英]Function doesn't run in Javascript

I use a sample code to find a string in main string. 我使用示例代码在主字符串中找到一个字符串。 But when I run it on editor online, it work fine. 但是,当我在在线编辑器上运行它时,它可以正常工作。 But when I run it in local host XAMPP, it doesn't work. 但是,当我在本地主机XAMPP中运行它时,它不起作用。

I try to comment all and just only alert(), it work. 我尝试全部注释,仅注释alert(),它起作用。 But when I run default code, it return nothing. 但是,当我运行默认代码时,它什么也不返回。

The main problem in here is that, when I run it in online editor like w3schools, it can work and return the results fine. 这里的主要问题是,当我在w3schools之类的在线编辑器中运行它时,它可以正常工作并返回结果。 But when I run it on my machine (I use XAMPP), it totally return nothing. 但是,当我在计算机上运行它(使用XAMPP)时,它完全不返回任何内容。

This is my code here: 这是我的代码在这里:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to extract characters from the string.</p>
<button onclick="findNewProgrammingLanguage()">Try it</button>
<p id="demo"></p>
<script>
    function findNewProgrammingLanguage() {
        var programminglanguages = "C++, JavaScript, Ruby";
        var newlanguage = "Python";
        if (programminglanguages.indexOf(newlanguage) >=0) {
            document.getElementById("demo").innerHTML = language;
        } else {
            document.getElementById("demo").innerHTML = "No";
        }
    }
</script>
</body>
</html>

https://www.w3schools.com/code/tryit.asp?filename=FO6C714HYD7C https://www.w3schools.com/code/tryit.asp?filename=FO6C714HYD7C

Inside the if statement, which is the value of language ? 在if语句中, language的价值是什么?

if (programminglanguages.indexOf(newlanguage) >=0) {   
  document.getElementById("demo").innerHTML = language; // language is not defined
}

I think that's the problem 我认为这就是问题所在

Finally I can solved what problem here. 最后我可以在这里解决什么问题。 It have some confusing with config file in XAMPP. 它与XAMPP中的配置文件有些混淆。 I try to turn off it and restart. 我尝试将其关闭并重新启动。 And then it work! 然后工作!

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

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