javascript - FileReader.onload 怎么传递额外的参数给onload函数
PHP中文网
PHP中文网 2017-04-11 09:00:36
[JavaScript讨论组]

这是官方文档的用法:

// Callback from a <input type="file" onchange="onChange(event)">
function onChange(event) {
  var file = event.target.files[0];
  var reader = new FileReader();
  reader.onload = function(event) {
    // The file's text will be printed here
    console.log(event.target.result)
  };

  reader.readAsText(file);
}

但如果我想传递一个参数给function(event){}, 应该怎么做,我试过如下定义:

var a = 1;
reader.onload = function(event, a) {
    // The file's text will be printed here
    console.log(event.target.result)
  };
PHP中文网
PHP中文网

认证0级讲师

全部回复(2)
黄舟

传递参数给function是你主动执行了funtion才能传递,类似onload 的触发机制其实是被动的,里面的参数也就固定了,想要在onload 里面使用其它参数就自己在外部定义,然后里面使用

PHPz
var a = 1;
reader.onload = function(event) {
    console.log(a); // 这里直接使用
    console.log(event.target.result)
};
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号