javascript - JS复制输入的值

标签 javascript input

我怎样才能用我在输入字段上写的任何内容来写 h1?

是这样的:

<input id="inputtxt" type="text" value="Insert text here..">
<h1 id="newtxt"></h1>

<script>
var txtelement

txtelement = document.getElementById("inputtxt").value;
document.getElementById("newtxt").innerHTML=txtelement;

</script>

它在页面加载时复制,但在我编写时不复制

最佳答案

你可以使用 .oninput 事件:

document.getElementById("inputtxt").oninput = function(e){
    document.getElementById("newtxt").innerHTML=e.target.value;
};

注意:您可以使用占位符而不是值来向输入字段内的用户显示默认消息。

希望这对您有所帮助。


document.getElementById("inputtxt").oninput = function(e){
  document.getElementById("newtxt").innerHTML=e.target.value;
};
<input id="inputtxt" type="text" placeholder="Insert text here..">
<h1 id="newtxt"></h1>

关于javascript - JS复制输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35750909/

相关文章:

javascript - 带有 Node.js 的 ASP.NET Core 2.2 应用程序在 VS 更新后中断

Javascript - 将多个动态值添加到 for 循环中的动态键

ios - Unity - Steel Series Nimbus 输入

javascript - 我可以使用原生 JS 手动更改 DOM 的 &lt;input type ="color"> 实际可显示颜色吗?

c - 按 Enter 继续 C

javascript - JavaScript 中的 typeof() 函数错误

javascript - Meteor 响应式(Reactive)搜索代码未运行

c - 输入两个数字的值

matlab - 如何在不读取字符串的情况下检查输入字符串格式的正确性?

javascript - React 传播属性列表