javascript - 如何在函数之外使用变量?
问题描述
function ofInput() {
const data = Array.from(
this.children,
tr => Array.from(tr.querySelectorAll('input'), input => input.value)
);
}
document.querySelector('tbody').addEventListener('input', ofInput);
console.log(data);
该表在每个单元格中都有一个输入。将数据输出到输入时,数组中的数据应自动更改
解决方案
The data variable is inside the scope of the function ofInput. You need to declare the variable outside of the scope of function to access it. Try this out:
let data;
function ofInput() {
data = Array.from(
this.children,
tr => Array.from(tr.querySelectorAll('input'), input => input.value)
);
}
document.querySelector('tbody').addEventListener('input', ofInput);
if (data && typeof(data) === "array") console.log(data);
else console.log("Event did not occur");
推荐阅读
- google-apps-script - 如何为免费版 Google Add On 添加使用限制
- python - 带有自定义 Estimator 的 TensorFlow 指标
- python - 谷歌应用引擎部署烧瓶应用 - ImportError:libSM.so.6:无法打开共享对象文件:没有这样的文件或目录
- mysql - 使用 Node.js 和 MySQL 时服务器响应出现 TypeError
- python - Django 发出多对多变化的信号
- java - 覆盖/绕过 Spring Boot 默认登录页面到 okta OIDC 登录页面
- java - 编译器找不到枚举类型。(战舰游戏)
- python - 在单独的线程中执行时出现 ValueError
- java - 我关于制作强字符串密码的代码给出了运行时错误如何解决?
- mysql - 对值进行 ASC 排序,然后通过排序表的索引得到对应的值