脚本不适用于JQuery 1.9 [英] Script does not work with JQuery 1.9

查看:94
本文介绍了脚本不适用于JQuery 1.9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很努力尝试找出此脚本中与JQuery 1.9.0不兼容的内容.它在1.7.2下可以正常工作.

I am really struggling trying to find out what in this script is not JQuery 1.9.0 compatible. It works fine with 1.7.2.

应该拍摄一张用户选择上传的图片,并在实际上传之前使用HTML5展示它.

It's supposed to take a picture the users chooses to upload and show it using HTML5 before actually uploading it.

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

I hope someone have a sharp eye to spot the error!

// 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);
}

推荐答案

jQuery 1.9删除了以前不推荐使用的所有旧功能.

jQuery 1.9 has dropped a whole load of old features that had previously been deprecated.

我建议您阅读jQuery提供的说明,以将其从以前的版本升级到1.9.有很多东西已经被删除或更改,所以除非您一直保持最新的推荐最佳实践,否则您很可能会被某些东西吸引住.

I suggest you read the notes provided by jQuery for upgrading to 1.9 from previous releases. There's quite a lot of stuff that's been removed or changed, so unless you've been keeping right up-to-date with their recommended best practices, you are quite likely to be caught out by something.

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

这篇关于脚本不适用于JQuery 1.9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆