在HTML中调用JavaScript函数

3
我正在尝试从JavaScript中打印一些内容到我的HTML页面上。我已经尝试了以下代码,但它没有生效:
<!DOCTYPE html>
<html>
<head>

</head>
<body>
<p>I am trying to print <script>go();</script></p>



<script>

function go() {
 document.write("Hello World!");
}
</script>
</body>
</html>
1个回答

4

您在发布的示例中尚未定义函数,因此对go的调用实际上没有任何作用,请更改脚本标记的顺序。

<!DOCTYPE html>
<html>
<head>

<script>

function go() {
 document.write("Hello World!");
}
</script>

</head>
<body>
<p>I am trying to print <script>go();</script></p>



</body>
</html>

非常感谢 :) 已经解决了问题,它可以工作。 - SOURAV

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接