当前位置:主页 > 网页前端 > JavaScript代码 >

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

时间:2021-04-29 10:56:18 | 栏目:JavaScript代码 | 点击:

实例如下所示:

$(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);
})

您可能感兴趣的文章:

相关文章