我有一个<p>
元素,该元素应该包含任何在<input>
框中写入的内容。 <p>
包含<input>
除最后一个字符外的所有内容。html元素不会根据输入值更新
例如,(不是有效的JS代码)
if input = Fish
then p = Fis
if input = Monster
then p = Monste
这里是我的代码
<head>
<script>
window.onload = function()
{
document.querySelector('input').addEventListener('keydown', function()
{
document.querySelector('p').innerHTML = document.querySelector('input').value
});
}
</script>
</head>
<body>
<p></p>
<input type="text" />
</body>
如何让p
元素包含即使在input
的最后一个字符?
更好事件'input'。 – trincot
“更好”是主观术语,但是是的,这也是一种选择。 – Paul