脚本不适用于JQuery 1.9

时间:2013-01-18 14:08:34

标签: javascript jquery

我真的在努力寻找这个脚本中不兼容JQuery 1.9.0的东西。它适用于1.7.2。

应该拍摄用户选择上传的照片,并在实际上传之前使用HTML5进行显示。

我希望有人能够敏锐地发现错误!

// convert bytes into friendly format
function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB'];
    if (bytes == 0) return 'n/a';
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};

// check for selected crop region
function checkForm() {
    if (parseInt($('#w').val())) return true;
    $('.error').html('Please select a crop region and then press Upload').show();
    return false;
};

// update info by cropping (onChange and onSelect events handler)
function updateInfo(e) {
    $('#x1').val(e.x);
    $('#y1').val(e.y);
    $('#x2').val(e.x2);
    $('#y2').val(e.y2);
    $('#w').val(e.w);
    $('#h').val(e.h);
};

// clear info by cropping (onRelease event handler)
function clearInfo() {
    $('.info #w').val('');
    $('.info #h').val('');
};

function fileSelectHandler() {

    // get selected file
    var oFile = $('#image_file')[0].files[0];

    // hide all errors
    $('.error').hide();

    // check for image type (jpg and png are allowed)
    var rFilter = /^(image\/jpeg|image\/png)$/i;
    if (! rFilter.test(oFile.type)) {
        $('.error').html('Please select a valid image file (jpg and png are allowed)').show();
        return;
    }

    // check for file size
    if (oFile.size > 250 * 1024) {
        $('.error').html('You have selected too big file, please select a one smaller image file').show();
        return;
    }

    // preview element
    var oImage = document.getElementById('preview');

    // prepare HTML5 FileReader
    var oReader = new FileReader();
        oReader.onload = function(e) {

        // e.target.result contains the DataURL which we can use as a source of the image
        oImage.src = e.target.result;
        oImage.onload = function () { // onload event handler

            // display step 2
            $('.step2').fadeIn(500);

            // display some basic image info
            var sResultFileSize = bytesToSize(oFile.size);
            $('#filesize').val(sResultFileSize);
            $('#filetype').val(oFile.type);
            $('#filedim').val(oImage.naturalWidth + ' x ' + oImage.naturalHeight);

            // Create variables (in this scope) to hold the Jcrop API and image size
            var jcrop_api, boundx, boundy;

            // destroy Jcrop if it is existed
            if (typeof jcrop_api != 'undefined') 
                jcrop_api.destroy();

            // initialize Jcrop
            $('#preview').Jcrop({
                aspectRatio : 178 / 200, // keep aspect ratio 1:1
                minSize: [178, 200],
                boxWidth: 534,
                boxHeihgt: 600,

                bgFade: true, // use fade effect
                bgOpacity: .3, // fade opacity
                onChange: updateInfo,
                onSelect: updateInfo,
                onRelease: clearInfo
            }, function(){

                // use the Jcrop API to get the real image size
                var bounds = this.getBounds();
                boundx = bounds[0];
                boundy = bounds[1];

                // Store the Jcrop API in the jcrop_api variable
                jcrop_api = this;
            });
        };
    };

    // read selected file as DataURL
    oReader.readAsDataURL(oFile);
}

2 个答案:

答案 0 :(得分:4)

jQuery 1.9已经删除了之前已被弃用的一大堆旧功能。

我建议你阅读jQuery提供的注释,以便从以前的版本升级到1.9。有很多东西被删除或更改,所以除非你一直保持最新的推荐最佳实践,否则你很可能被某些东西搞得一团糟。

以下是升级指南:http://jquery.com/upgrade-guide/1.9/

答案 1 :(得分:0)

Line 4: if (bytes == 0) return 'n/a'; --- Use '===' to compare with '0'.
Line 5: var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); --- Missing radix parameter.
Line 11: if (parseInt($('#w').val())) return true; --- Missing radix parameter.
Line 88: bgOpacity: .3, // fade opacity --- A leading decimal point can be confused with a dot: '.3'.

所有人都指出here