简体   繁体   English

Javascript无法在Visual Studio中使用

[英]Javascript not working Visual Studio

My current code is: 我当前的代码是:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>App1</title>
    <link href="css/default.css" rel="stylesheet" />
</head>
<body>
    <p id="test">Content goes here!</p>
    <button onclick="myFunction()">Try it</button>
    <script>  
        function myFunction() {
            document.getElementById("test").innerHTML = "Working";
        }
    </script>
</body>
</html>

When I debug on Visual Studio 2017 the html works fine. 当我在Visual Studio 2017上调试时,html可以正常工作。 Picture of code execution 代码执行图

However, clicking the button does nothing. 但是,单击按钮不会执行任何操作。 No error messages, just nothing happens. 没有错误消息,什么也没有发生。 If I copy and paste the code into a text document, format it as an html and test that in Chrome, everything works fine. 如果我将代码复制并粘贴到文本文档中,将其设置为html格式并在Chrome中进行测试,则一切正常。 Any idea why this happens? 知道为什么会这样吗?

Edit: It was made as a Blank JavaScript App if that changes anything 编辑:如果更改了任何内容,则将其作为空白JavaScript应用制作

I can't say why that would be the case. 我不能说为什么会这样。 There is nothing in your code that would indicate an issue, other than it is possible that the file open in your browser is NOT the file you are editing in Visual Studio. 您的代码中没有任何东西表明存在问题,除了浏览器中打开的文件可能不是您在Visual Studio中编辑的文件之外。

Make sure that, from Visual Studio, you right click on the source code and choose to run the page in a browser. 确保从Visual Studio中右键单击源代码,然后选择在浏览器中运行页面。

I can say that you should not be using inline HTML event attributes ( onclick , etc.), here's why. 我可以说您不应该使用内联HTML事件属性( onclick等), 这就是原因。

Try reformatting your code to use modern, standards-based code like this: 尝试重新格式化代码,以使用如下所示的基于现代标准的代码:

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>App1</title> <link href="css/default.css" rel="stylesheet" /> </head> <body> <p id="test">Content goes here!</p> <button id="btn">Try it</button> <script> // Get DOM references var p = document.getElementById("test"); var btn = document.getElementById("btn"); btn.addEventListener("click", myFunction); function myFunction() { p.innerHTML = "Working"; } </script> </body> </html> 

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

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