gpt4 book ai didi

javascript - 如何实时捕获击键并将其添加到 div 元素?

转载 作者:行者123 更新时间:2023-11-29 18:03:43 25 4
gpt4 key购买 nike

在下面的代码中,我试图获取按键并将其添加到我的 div 元素中。例如:如果我按随机键,如“fhjbfwjbj”,它应该被添加到 div 中的文本中。 :

<script>
function type()
{
var edit = document.getElementById("add");
edit.innerHTML=edit.innerHTML + //WHAT TO ADD?????;
}
</script>

<body onkeypress="type()">
<div id="add" style="position:relative; left:600px; top:170px;">

Add to this text

</div>
</body>

最佳答案

像这样:

document.body.onkeypress = function(evt) {
var edit = document.getElementById("add");
edit.innerHTML=edit.innerHTML + String.fromCharCode(evt.which || evt.keyCode);
}

关于javascript - 如何实时捕获击键并将其添加到 div 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com