javascript - 返回我在 HTML 表单中键入的文本

标签 javascript html

如何在不使用任何 JavaScript 库的情况下在文本字段中键入内容时获取文本字段的数据?像这样在 AngularJS 中实现了类似的东西:

<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!</h1>

最佳答案

如果没有任何框架/库,您必须将 keyup 事件绑定(bind)到 input 元素,并且在您必须更新的 event 处理程序中DOM。

document.querySelector("input").addEventListener("keyup", function() {
    document.querySelector("h1 strong").textContent= this.value;
});
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here" value="">
<hr>
<h1>Hello <strong></strong></h1>

关于javascript - 返回我在 HTML 表单中键入的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282014/

相关文章:

javascript - Angular ng-repeat动态对象数组

javascript - 倒计时器可额外增加 2 分钟

javascript - IE 无法在单选按钮上使用 "return false"

html - 自动将相邻的 div 居中

javascript setTimeout 没有做我期望的事情

javascript - 如何张贴提及松弛传入的 webhooks

jquery - float 标签未显示输入焦点上的标签

html - 在移动 chrome 浏览器中页面右侧显示不需要的空间,但在移动 firefox 浏览器中显示正常

javascript - 如何在gridfs-stream中使用monk启动的mongoDB实例?

ruby-on-rails - Rails 应用程序的移动友好音频播放器?