<!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>