按钮onclick无法与外部JS一起正常工作



这被分隔在两个文档中,一个是html,另一个是js。我使用VSC作为编辑器。这是我的HTML第一个:

我正试图把印刷品印出来,说";你好,约翰;在我点击浏览器上的按钮之后。然而,它的唯一工作方式是当它与按钮同时上升时,但我需要它直到之后才能返回。

这可能是一个非常简单的解决方案,但我还没能找到确切的方法,而且我是一名新的JavaScript学生,所以我还不能胜任搜索问题。(在调试中,它甚至不会将其视为错误或任何东西,所以我甚至不能在谷歌中复制和粘贴。(

function hello() {
var name = "John";
return "Hello " + name;
}
document.getElementById("well").innerHTML = hello();
<p id="well"></p>
<button onclick="hello()">
Click me for an example of the return keyword in action!!!
</button>

您需要在hello函数中设置innerHTML,如下例所示:

function hello() {
var name = "John";
document.getElementById("well").innerHTML = "Hello " + name;
}
<p id="well"></p>
<button onclick="hello()">
Click me for an example of the return keyword in action!!!
</button>

最新更新