如何在不使用任何 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/