您的位置:首页 > Web前端 > JavaScript

JS获取input[file]的值并显示在页面的方法

2017-04-05 17:32 435 查看
$(document).on('change', '.photo-box .file', function () {
    //alert($(this).val());
    function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL != undefined) { // basic
            url = window.createObjectURL(file);
        } else if (window.URL != undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
    var objUrl = getObjectURL(this.files[0]);
    console.log("objUrl = " + objUrl);
    var html = '<div class="photo-box"><img src="' + objUrl + '" alt=""><div class="photo-btn"><p>删除</p></div></div>';
    $(this).parent().parent().append(html);
})

代码如图所示,获取到url后再添加到页面写好的模块里面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: