【问题标题】:Javascript - return Console/DOM output as a single JSON outputJavascript - 将控制台/DOM 输出作为单个 JSON 输出返回
【发布时间】: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


【解决方案1】:

好的,第一件事:

您提供给我们的输出示例在任何 JSON 规范中均无效,并且不作为 JavaScript 中的对象进行评估(因为错误使用了引号)。

您想要的是一个具有属性“onkeyup”(已处理事件的名称)的对象,该属性包含一个对象数组(在您的情况下,是该事件的发生)。

let obj = { onkeyup: [] };

window.onload = function() {
    const myInput = document.getElementById('myInput');

    myInput.onkeyup = function(e) {
        obj.onkeyup.push(e);
        console.log( obj );
    }

}

Sample output

这可能会解决您的问题,方法是让对象具有所需的结构,您可以根据需要在代码中进一步更新和使用。

【讨论】:

    【解决方案2】:

    也许是这样的?

        // new Array to store events
        let events = []
        window.onload = function() {
            const myInput = document.getElementById('myInput');
            myInput.onkeyup = function(e) {
                //console.log(e);
                //Push events into array
                events.push(e)
                // Log events array
                console.log({events})
            }
        }
    <input type="text" value="" id="myInput">

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      相关资源
      最近更新 更多