猿问

使用阅读器 jquery 时图像未显示

图像未显示,,,代码选取图像的相对路径而不是绝对路径


 readURL(input) {

    if (input.files && input.files[0]) {

        var reader = new FileReader();

        reader.onload = function(e) {

            $('#blah')

                .attr('src', e.target.result.toString);

        };

        reader.readAsDataURL(input.files[0]);

  }


侃侃尔雅
浏览 63回答 1
1回答

Smart猫小萌

你有一个小的语法错误,应该作为一个方法调用:toString()&nbsp;function readURL(input) {&nbsp; &nbsp; if (input.files && input.files[0]) {&nbsp; &nbsp; &nbsp; &nbsp; var reader = new FileReader();&nbsp; &nbsp; &nbsp; &nbsp; reader.onload = function(e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#blah')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .attr('src', e.target.result.toString());&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; reader.readAsDataURL(input.files[0]);&nbsp; &nbsp; }&nbsp;}$('#myFile').on('change', function(){&nbsp; &nbsp; &nbsp; readURL(event.target);&nbsp;});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input type="file" id="myFile"/><img src="" id="blah" style="height:50px;width:50px;"/>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答