【发布时间】:2019-10-17 11:45:05
【问题描述】:
在我的页面中,我添加了一个onkeyup 事件,以获取在我的input 中按下的键。
我在控制台中获得了结果,但我需要将所有 事件 输出为单个 JSON 输出和 return 它。
我是 JS 的新手,感谢您提供任何帮助
PS:由于某种原因,我的 sn-p 在这里不起作用,但在我的 index.html 中,我可以看到事件输出正常。
window.onload = function() {
const myInput = document.getElementById('myInput');
myInput.onkeyup = function(e) {
console.log(e);
}
}
<input type="text" value="" id="myInput">
电流输出:
{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}
而我正在寻找的是能够将其分配到一个变量,例如var json,输出将是:
{
"onkeyup":
"{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "a", code: "KeyA", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "s", code: "KeyS", location: 0, ctrlKey: false, …}
{isTrusted: true, key: "d", code: "KeyD", location: 0, ctrlKey: false, …}"
}
提前致谢!
【问题讨论】:
-
将所有事件输出为单个 JSON 输出并返回。 什么样的输出?回到什么?能举个例子吗?
-
var a = []; a.push(e) -
@sa_n__u 谢谢,我一开始是这样做的,但仍然是单一的,而不是我在问题中添加的输出示例
-
@Rubioli
var a = {'onKeyup':[]}; a.onKeyup.push(e)
标签: javascript json console output onkeyup