gpt4 book ai didi

javascript - 如何正确调用 JavaScript 函数?

转载 作者:行者123 更新时间:2023-12-04 09:42:36 24 4
gpt4 key购买 nike

<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Example</title>
<script>

function displayString() {
return "<h1>Main Heading</h1>"
}

displayString();
document.write("Execute during page load from the head<br>");
</script>
</head>

<body>

<script>
document.write("Execute during page load from the body<br>");
</script>


</body>
</html>

所以这是我的问题。无论我把 displayString() 放在哪里,h1 似乎都不会出现在浏览器上。有人可以帮我看看我错在哪里吗?我是 JavaScript 的新手。哦,我想做的是调用该函数。

最佳答案

您需要将返回的字符串写入 document :

<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Example</title>
<script>

function displayString() {
return "<h1>Main Heading</h1>"
}

document.write(displayString());
document.write("Execute during page load from the head<br>");
</script>
</head>

<body>

<script>
document.write("Execute during page load from the body<br>");
</script>


</body>
</html>

关于javascript - 如何正确调用 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62261674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com