代码之家  ›  专栏  ›  技术社区  ›  Miel

附加文本立即消失

  •  0
  • Miel  · 技术社区  · 15 年前

    这是一个小例子:

    <html>
      <head>
        <script>
          function show() {
            var newName = document.showMe.textToShow.value;
            var txt = document.createTextNode(newName);
            document.getElementById("feedback").appendChild(txt);
          }
        </script>
      </head>
      <body>
        <form name="showMe">
          Name: <input type="text" name="textToShow" /><br />
          <input type="submit" value="Show" OnClick="show()" />
        </form>
        <div id="feedback"></div>
      </body>
    </html>
    

    我做错什么了?

    谢谢,

    米埃尔。

    1 回复  |  直到 15 年前
        1
  •  2
  •   thejh    15 年前

    它可以工作,但随后表单会被提交。你必须禁止这样发送表单

    <input type="submit" value="Show" OnClick="show(); return false" />
    

    或使用按钮:

    <button type="button" onclick="show()">Show</button>