javascript - 你会如何在输入内容时让预先写好的文本出现?

标签 javascript html css

我正在制作一个互动故事,我想让读者在一个字段中输入一些东西,但不是他们输入的任何内容,而是输入一条预先写好的消息。它看起来像 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/

相关文章:

javascript - 将结果从 php if 语句传递到 javascript/html

javascript - 如果 window.location.href 不匹配某个字符串,则将 css 类添加到元素

javascript - 有没有一种方法可以在 Google 的 Materialise CSS 中创建垂直旋转木马而无需自定义构建?

jquery - 如何在申请下一个类之前等待 css 转换完成

html - 谷歌浏览器无法正确呈现字体

javascript - 如何在 d3.js 中的兄弟元素之后插入

javascript - 更新具有不同值的多个文档

python - 使用 Jinja2 的 HTML 模板 - 丢失

html - 隐藏 <div> 中的 <a> 问题

javascript - 连接 NextJS、next-i18next、with-redux、with-redux-saga : "Error: If you have a getInitialProps method in your custom _app.js file..."