Javascript-创建打印到HTML的函数不起作用

Javascript - Creating a print to HTML function is not working

本文关键字:函数 不起作用 HTML 创建 打印 Javascript-      更新时间:2023-09-26

我正在尝试创建一个"printf"函数,该函数将接受输入"x"并输出结果数据,以便在"id"为"para"的"p"标记内打印。但我似乎不明白为什么它不起作用。我不知道我是否应该将"this"与"x"结合使用。但我不知道。

这是我的JS:

function printf(x) {
    document.getElementById("para").innerHTML = x;
};
printf("Hello!");

这是我的HTML:

<div class="play">
    Input:<input type="text" id="userInput" />
    <button type="button" id="submit" onclick="check()">Submit</button>
    <div id="open">
        <h3>Terminal:</h3>
        <p id="para"></p>
    </div>
    <h1>Time to play a game!</h1>
</div>

这是您的工作代码。您已错过return

function printf(x) {
  return document.getElementById("para").innerHTML = x;
};
printf("Hello!");
<div class="play">
  Input:
  <input type="text" id="userInput" />
  <button type="button" id="submit" onclick="check()">Submit</button>
  <div id="open">
    <h3>Terminal:</h3>
    <p id="para"></p>
  </div>
  <h1>Time to play a game!</h1>
</div>