码迷,mamicode.com
首页 > Web开发 > 详细

JS 图片预览功能

时间:2016-05-17 17:54:38      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

<script type="text/javascript">
    function DisplayImage(fileTag) {
        document.getElementById(‘viewfile‘).value = fileTag.value;
        fileTag.style.display = ‘none‘;
        var allowExtention = ".jpg.png.gif.JPG.PNG.GIF";
        var extentionArr = fileTag.value.split(‘.‘);
        var extention = extentionArr[extentionArr.length - 1];
        if (!(allowExtention.indexOf(extention) > -1)) {
            alert("请选择图片!");
        } else {
            //for adveced broswer(the newest ie,chrome,ff)
            if (typeof (FileReader) !== "undefined") {

                var reader = new FileReader();
                reader.readAsDataURL(fileTag.files[0]);
                reader.onload = function (e) {
                    document.getElementById("img_1").setAttribute("src", e.target.result);
                }
            } else {
                //for(ie6)
                document.getElementById("img_1").setAttribute("src", fileTag.value);
            }
        }
    }
</script>

JS 图片预览功能

标签:

原文地址:http://www.cnblogs.com/witeem/p/5502360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!