Prepend,在Jquery Uploader中附加<li> </li>

时间:2013-07-19 20:25:04

标签: javascript jquery html jquery-ui

我正在使用jQueryUI和jQuery BlueImp File Uploader。使用上传器,我想用jQuery UI创建拖放式UI对象。

但是,为了做到这一点,我需要在显示的文件的正面和背面附加<li></li>标记。例如,在下面的脚本中,我使用上传器显示上传的每个图像的缩略图。

如何添加li并将/li附加到正在显示的每张图片上?

<script>
/*jslint unparam: true, regexp: true */
/*global window, $ */
$(function () {
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = window.location.hostname === '' ?
                '' : 'server/php/',
        uploadButton = $('<button/>')
           // .addClass('btn')
            .prop('disabled', true)
          //  .text('Processing...')
           .on('click', function () {
                var $this = $(this),
                    data = $this.data();
                $this
                    .off('click')
                    .text('Abort')
                    .on('click', function () {
                        $this.remove();
                        data.abort();
                    });
                data.submit().always(function () {
                    $this.remove();
                });
            });
    $('#fileupload').fileupload({
        url: url,
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png|mp4)$/i,
        maxFileSize: 50000000, // 5 MB
        // Enable image resizing, except for Android and Opera,
        // which actually support image resizing, but fail to
        // send Blob objects via XHR requests:
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 100,
        previewMaxHeight: 100,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        data.context = $('<div/>').appendTo('#files');
        $.each(data.files, function (index, file) {
            var node = $('<p/>')
                    //.append($('<span/>').text(file.name));
            if (!index) {
                node
                    .append('')
                  //  .append(uploadButton.clone(true).data(data));
            }
            node.appendTo(data.context);
        });
    }).on('fileuploadprocessalways', function (e, data) {
        var index = data.index,
            file = data.files[index],
            node = $(data.context.children()[index]);
        if (file.preview) {
            node
                .prepend('')
                .prepend(file.preview);
        }
        if (file.error) {
            node
                .append('<br>')
                .append(file.error);
        }
        if (index + 1 === data.files.length) {
            data.context.find('button')
              //  .text('Upload2')
                .prop('disabled', !!data.files.error);
        }
    }).on('fileuploadprogressall', function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .bar').css(
            'width',
            progress + '%'
        );
    }).on('fileuploaddone', function (e, data) {
         $.each(data.result.files, function (index, file) {
        //    var link = $('<a>')
               // .attr('target', '_blank')
             //   .prop('href', file.url);
            $(data.context.children()[index])
              //  .wrap(link);
        });
    }).on('fileuploadfail', function (e, data) {
        $.each(data.result.files, function (index, file) {
            var error = $('<span/>').text(file.error);
            $(data.context.children()[index])
                .append('<br>')
                .append(error);
        });
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
});
</script>

0 个答案:

没有答案