【发布时间】:2018-12-28 04:07:34
【问题描述】:
我是 Javascript 新手,知道如何在执行 javascript 之前加载 html。
我确实检查了有关 stackoverflow 的问题,并尝试实施给出的解决方案,但没有奏效。
以下是我尝试过的各种代码的代码。
在正文结束前写脚本标签
在函数中编写了 javascript 代码并在 body 的 onload 中调用它,但它也不起作用。 (还尝试评论 documnet.onload = cal(); 并执行)。
<!DOCTYPE html>
<html>
<head>
<title>Age Calculator</title>
</head>
<body onload="cal();">
<h1>Age Calculator</h1>
<p>Enter your age and find how many days you have been alive</p>
<script type="text/javascript" src="Age.js"></script>
</body>
</html>
//Javascript code
function cal(){
var age = prompt("How old are you?");
var days = age * 365.25;
alert("You are approx " + days + " days");
}
documnet.onload = cal();
我希望在询问用户你几岁之前显示 html?
【问题讨论】:
-
开发时始终打开浏览器控制台。这样你会看到像 "Uncaught ReferenceError: documnet is not defined" 之类的错误(仅供参考,它是
document) -
看看小提琴jsfiddle.net/vks9009/8w24k3xv。如果长 html 代码,此代码也将起作用
标签: javascript html