我怎样才能用我在输入字段上写的任何内容来写 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/