简体   繁体   English

jQuery裁剪器不适用于动态生成的图像

[英]jQuery cropper doesn't work on dynamically generated image

I have just downloaded this jQuery plugin that helps in cropping an image. 我刚刚下载了这个jQuery插件 ,可帮助裁剪图像。 I have the following function which is called on an file input field's change event: 我有以下在file输入字段的change事件上调用的函数:

function loadPreview(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $(".photoBox div").hide();
            var cropBox = $(".photoPreview").show();
            var $img = $(document.createElement('img'));
            $img.attr("src", e.target.result);
            cropBox.append($img);

            $(".photoPreview img").cropper({
                aspectRatio: 1,
                dashed: false,
                zoomable: false,
                rotatable: false
            });
        };

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

The cropper code doesn't show up in Mozilla, but in Opera it does. 裁剪器代码未在Mozilla中显示,但在Opera中却显示。 Where am I wrong? 我哪里错了?

Try to use createObjectURL func instead of FileReader: 尝试使用createObjectURL func代替FileReader:

imageUrl = window.URL.createObjectURL(input.files[0])
$img.attr("src", imageUrl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM