На своей странице я добавил событие onkeyup
, чтобы получить клавиши, которые были нажаты в моем input
.
Я получаю результаты в моемконсоль, но мне нужно вывести все события как один JSON
вывод и return
it.
Я новичок в JS
, и любая помощьоценил
PS: По какой-то причине мой фрагмент здесь не работает, но в моем 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, …}"
}
Заранее спасибо!