首页 > 代码库 > JS图片上传预览

JS图片上传预览

HTML部分:

<img id="avatar" class="editable img-responsive" alt="头像" src="/static/avatar/user.png" style="width: 100px;height: 100px;" /><input id="image" name="image" type="file" style="display: none">

JS部分:

<script>$(‘#avatar‘).click(function(){        $("#image").trigger(‘click‘);    });$(‘#image‘).on(‘change‘,function(){       var imgdate = $(this).get(0);       var showing = document.getElementById(‘avatar‘);       if(imgdate.files && imgdate.files[0]){           showing.src = window.URL.createObjectURL(imgdate.files[0]);       }});</script>

 

JS图片上传预览