JS上传图片选择后立即预览

<html>
<head>
<title>测试</title>
<script type="text/javascript">
    window.onload = function () {
        if (window.attachEvent) {
            document.getElementById("file1").attachEvent('onpropertychange', function (o) {
                document.getElementById("imgID").src = 'file:///' + document.getElementById("file1").value;
            });
        }
        else {
            document.getElementById("file1").onchange = function () {
                document.getElementById("imgID").src = window.URL.createObjectURL(document.getElementById("file1").files[0]);
            }
        }
    }
</script>
</head>
<body>
<div>
      <img id="imgID" src="C:\fakepath\psb.jpg" />
</div>
<div>
       上传:<input type="file" id="file1" />
</div>
</body>
</html>

 

posted @ 2013-10-11 19:30  瞭望者  阅读(457)  评论(0编辑  收藏  举报