为什么';t会影响这段JavaScript代码吗?
我读了一本JavaScript书,它给了你一段代码来运行,但当我读的时候,我什么都没有得到。它运行但不显示任何内容为什么';t会影响这段JavaScript代码吗?,javascript,Javascript,我读了一本JavaScript书,它给了你一段代码来运行,但当我读的时候,我什么都没有得到。它运行但不显示任何内容 var theNumber = Number(prompt("Pick a number", "")); alert("Your number is the square root of " + theNumber * theNumber); 我在Harp项目中运行这段代码,其中\u layout.jade看起来像: doctype html head link(
var theNumber = Number(prompt("Pick a number", ""));
alert("Your number is the square root of " +
theNumber * theNumber);
我在Harp项目中运行这段代码,其中\u layout.jade
看起来像:
doctype
html
head
link(rel="stylesheet" href="/main.css")
body
!= yield
<script src="main.js" type="text/javascript"></script>
我知道配置是可以的,因为在此之前我已经运行了另一段代码。谷歌浏览器运行代码,但不显示任何窗口。我以为这可能是一个原因,但事实并非如此。有人知道会发生什么吗?
谢谢如果您在chrome中打开.js文件,您只需将其视为原始文本即可。您需要将其包含在html页面中,如下所示:
<html>
<head>
<script type='text/javascript'>
window.onload = function()
{
var theNumber = Number(prompt("Pick a number", ""));
alert("Your number is the square root of " + (theNumber * theNumber));
}
</script>
</head>
<body>
<!-- page content, if any -->
</body>
</html>
按F12键,在控制台中查找错误消息。在此处运行:-您是否包括js?指向file.js的路径是否正确?代码很好。甚至在堆栈代码段中运行。您如何知道它运行?你说的“我用Chrome检查了它”到底是什么意思?这段JavaScript绝对有效——在我运行它时工作正常。你的.js代码没有被执行。把它放在
窗口中。onload
处理程序,如我编辑的答案所示。仍然不起作用。我知道代码是被执行的,因为如果我键入console.log,它就会工作
<html>
<head>
<script type='text/javascript'>
window.onload = function()
{
var theNumber = Number(prompt("Pick a number", ""));
alert("Your number is the square root of " + (theNumber * theNumber));
}
</script>
</head>
<body>
<!-- page content, if any -->
</body>
</html>
<html>
<head>
<script type='text/javascript' src='script.js'></script>
</head>
<body>
<!-- page content, if any -->
</body>
</html>
window.onload = function()
{
var theNumber = Number(prompt("Pick a number", ""));
alert("Your number is the square root of " + (theNumber * theNumber));
}