• html5上传图片后在网页显示出来,自定义file的样式


    参考:https://blog.csdn.net/milijiangjun/article/details/79723013

    <!DOCTYPE html>
    <html lang="zh_CN">
    
        <head>
            <meta charset="UTF-8">
            <title>CSS样式:点击图片上传文件</title>
            <!-- 引入样式 -->
            <!--<link rel="stylesheet" type="text/css" href="css/style.css">-->
            <style type="text/css">
                /*定义图像以及大小*/
                
                .imageFileInput {
                     200px;
                    height: 200px;
                    position: absolute;
                    background-color: red;
                    /*这里可以换成图片路径(background-image:../img/....)注意图片路径*/
                }
                /*定义上传*/
                
                .fileInput {
                    height: 100%;
                    position: absolute;
                    right: 0;
                    top: 0;
                    opacity: 0;
                }
            </style>
        </head>
    
        <body>
            <div class="imageFileInput" id="result">
                <!--<input class="fileInput" type="file" id="" name="">-->
                <input type="file" id="file" class="fileInput" name="file" />
                <div id="result"></div>
            </div>
        </body>
    
    </html>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript">
        function ProcessFile(e) {
            var file = document.getElementById('file').files[0];
            if(file) {
                var reader = new FileReader();
                reader.onload = function(event) {
                    var txt = event.target.result;
                    var img = document.createElement("img");
                    img.src = txt; //将图片base64字符串赋值给img的src
                    // console.log(txt);
                    // document.getElementById("result").appendChild(img);
                    $('#result').css("background-image", "url(" + txt + ")");
    
                };
            }
            reader.readAsDataURL(file);
        }
    
        function contentLoaded() {
            document.getElementById('file').addEventListener('change',
                ProcessFile, false);
        }
    
        window.addEventListener("DOMContentLoaded", contentLoaded, false);
    </script>
  • 相关阅读:
    (转)很简短,但读完你会感触良多!
    (转)让 win8 快速通过认证的5个提示
    WPF 资源路径解析
    47、SimpleOrientationSensor
    45、SplashScreen
    让IE6也支持position:fixed
    utf8编码引起js输出中文乱码的解决办法(实用)
    javascript的currying函数
    sicily 1036. Crypto Columns
    sicily 6774. Buying Mortadella
  • 原文地址:https://www.cnblogs.com/ygyy/p/10476738.html
Copyright © 2020-2023  润新知