javascript 在 HTML 中调用 java 脚本函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28574926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:12:25  来源:igfitidea点击:

Calling a java script function inside HTML

javascripthtml

提问by SOURAV

I am trying to print something inside my html from a java script. I did this but it don't work:

我正在尝试从 java 脚本在我的 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>

采纳答案by konkked

You're function hasn't been defined yet in the example you are posting, so call to go effectively does nothing, change the ordering of your script tag

您在发布的示例中尚未定义函数,因此调用 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>