如何将javascript脚本更改为jquery功能代码

时间:2015-05-29 12:49:39

标签: javascript jquery

嘿伙计们是jQuery的新手,如何将这个javascript代码更改为jQuery功能代码,以便我随时随地调用它

LIKE:$("#profile_img").uploader();

显然这段代码工作正常,但我遇到的问题是每次需要在不同的文件输入上传中上传文件时都必须填充代码。

var input = document.getElementById("choosen_feeds_image"),
          formdata = false;

if (window.FormData) {
    formdata = new FormData();
    document.getElementById("feeds_upload_btn").style.display = "none";
}

if (input.addEventListener) {
    input.addEventListener("change", function (evt) {
        var i = 0, len = this.files.length, img, reader, file;

        document.getElementById("response").innerHTML = ""

        for (; i < len; i++) {
            file = this.files[i];
            if (!!file.type.match(/image.*/)) {
                if (window.FileReader) {
                    reader = new FileReader();
                    reader.onloadend = function (e) {
                        showUploadedItem(e.target.result);
                    };
                    reader.readAsDataURL(file);
                }
                if (formdata) {
                    formdata.append("feeds_image", file);
                }

                if (formdata) {
                    $.ajax({
                        url: "member/feeds_image_upload",
                        type: "POST",
                        data: formdata,
                        processData: false,
                        contentType: false,
                        success: function (res) {
                            if (res.length <= 40) {
                                document.getElementById('feeds_image_response').innerHTML = res;
                                $("#feeds_image_response").css('display', 'none');
                            } else {
                                document.getElementById("response").innerHTML = res;
                                $("#response").css('display', 'none');
                            }
                        }
                    });
                }
            } else {
                document.getElementById("response").innerHTML = "";
                alert("Sorry, You choose unsupported file");
            }
        }

    }), false
};

1 个答案:

答案 0 :(得分:0)

你可以在像这样的函数中输入所有内容

function uploader(){
    console.log('myFuntionUploader');   
}

然后像这样调用函数

uploader();