• 图片选择并使用base64展示


    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>图片上传选择</title>
        <style>
            .img-chooise,
            .img-chooise .ze {
                height: 150px;
                 150px;
            }
    
            .img-chooise {
                border: 1px solid #ccc;
                box-sizing: border-box;
                position: relative;
            }
    
            .img-chooise .ze {
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                background-color: rgba(0, 0, 0, 0.7);
                text-align: center;
                color: #1E90FF;
                display: none;
            }
    
            .img-chooise .button {
                list-style-type: none;
                -webkit-transition-duration: 0.4s;
                /* Safari */
                transition-duration: 0.4s;
                outline: none;
                border: 1px solid #1E90FF;
                color: #1E90FF;
                padding: 2px 10px;
                line-height: 150px;
                cursor: pointer;
            }
    
            .img-chooise .button:hover {
                background-color: #1E90FF;
                /* Green */
                color: white;
            }
    
            .set>div {
                float: left;
                margin-left: 20px;
                margin-bottom: 20px;
            }
        </style>
    </head>
    
    <body>
        <div class="set">
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
            <div class="img-chooise">
                <img src="images/plus.png" style="height: 100%;  100%;" />
                <div class="ze">
                    <a type="button" data-type="chooise" class="button">选择</a>
                    <a type="button" data-type="delete" class="button">删除</a>
                </div>
                <input type="file" style="display: none;" />
            </div>
        </div>
    </body>
    
    </html>
    
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
    
        $(function () {
    
            //显示与隐藏
            $(".img-chooise").on({
                mouseover: function () {
                    $(this).find(".ze").show();
                },
                mouseout: function () {
                    $(this).find(".ze").hide();
                }
            });
    
            //单击选择
            $(".img-chooise .ze a").click(function () {
                if ($(this).data("type") == "chooise") {
                    $(this).parent().next().trigger("click");
                } else if ($(this).data("type") == "delete") {
                    $(this).parent().prev().attr("src", "images/plus.png")
                }
            });
    
    
            //转base64
            $(".img-chooise input[type='file']").change(function (e) {
                var that = this;
                var file = e.currentTarget.files[0];//$(this).files[0];
    
                if (!/image/w+/.test(file.type)) {
                    alert("请确保文件为图像文件");
                    return false;
                }
                var reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = function (e) {
                    console.log(this.result);
    
                    $(that).prev().prev().attr("src", this.result);
                }
            })
    
    
        })
    </script>
    

      

    岁月无情催人老,请珍爱生命,远离代码!!!
  • 相关阅读:
    单变量微积分笔记19——数值积分
    单变量微积分笔记18——定积分的应用3(均值、权重、概率)
    同桌的你
    单变量微积分笔记17——定积分的应用2(体积)
    连通问题
    疯狂!!!
    单变量微积分笔记16——定积分的应用1(对数与面积)
    概率笔记1——独立事件下的简单概率
    上传图片流到服务器(AFN方法) (多张图片)(图片流)
    iOS开发通过AFNetworking上传图片到服务器
  • 原文地址:https://www.cnblogs.com/zhoushangwu/p/9083561.html
Copyright © 2020-2023  润新知