fileReader.onload里嵌套的 img.onload 里面获取不到外部的变量

代码如下:


<!DOCTYPE html>

<html>


<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>Document</title>

</head>


<body>

  <input type="file" onchange='fileChoosed(event)'>

  <img id='a'>

  <script>

    function fileChoosed(event) {

      console.log('aa');

      let file = event.target.files[0],

        fileName = file.name,

        img = document.getElementById('a'),

        fr = new FileReader(),

        ts = this;

      fr.addEventListener("load", () => {

        let blob = 'a';

        console.log(fileName, img, blob); //这里能获取到三个变量

        img.onload = (img, blob, fileName) => {

          debugger;

          view.insertPicture(img, blob, fileName); //这里就获取不到了

        };

        img.src = fr.result;

      });

      fr.readAsDataURL(file);

    }

  </script>

</body>


</html>

找了半天没找到原因,求帮助


慕丝7291255
浏览 502回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript