我正在制作一个互动故事,我想让读者在一个字段中输入一些东西,但不是他们输入的任何内容,而是输入一条预先写好的消息。它看起来像 hackertyper.net。
最佳答案
我构建了一个 JSFiddle用于您的问题陈述。
基本上,我所做的是将一些消息写到一个列表中,并从 0-list.length 生成随机索引,这样我就可以随机选择消息时尚。然后我将消息附加到用户按键时的文本区域。
开始在文本区域中输入,它的工作方式类似于 hackertyper.net。您可以将自己的消息添加到列表中。
这是代码。
var pre_msg = ["The brown", "Fox jumped", "over the well with", "or wait", "was it a pond", "well", "that's beside the point now.", "Oh and not to forget", "HACKED !"]
function myFunction() {
var index = Math.floor(Math.random() * pre_msg.length);
document.getElementById("text").value +=pre_msg[index]+" ";
}
<textarea id="text" style="width:300px;height:200px" value="" onkeypress="myFunction(); return false;"></textarea>
关于javascript - 你会如何在输入内容时让预先写好的文本出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45359261/