[英]How to use img src to browse and upload image
I want to do is to hide the 我要做的是隐藏
<input type='file' id="imgInp" accept="image/*" />
to the users and use the img tag to browse for image if the user clicked this specific img tag. 如果用户单击了此特定的img标签,则使用img标签浏览图像。 How do i do that using jquery? 我该如何使用jQuery?
current output: http://jsfiddle.net/LvsYc/3135/ 当前输出: http : //jsfiddle.net/LvsYc/3135/
$(document).ready(function() {
var currentSrc = $('#Picture').attr('src');
if(currentSrc==null || currentSrc==""){ $('#Picture').attr('src','http://i38.photobucket.com/albums/e149/eloginko/profile_male_large_zpseedb2954.jpg');
}
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#Picture').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
});
You can do that using CSS by removing opacity for the file input and position it over the image: 您可以使用CSS通过删除文件输入的不透明度并将其放置在图像上来做到这一点:
#form1{position:relative}
#imgInp{position:absolute;opacity:0;height:100%;top:0;cursor:pointer}
Browser variations may apply. 浏览器可能会有所不同。 Demo: http://jsfiddle.net/LvsYc/3138/ 演示: http : //jsfiddle.net/LvsYc/3138/
You could hide the input control 您可以隐藏输入控件
<input type='file' id="imgInp" accept="image/*" hidden="hidden" />
Set the image to use a link style cursor in CSS: 将图像设置为在CSS中使用链接样式光标:
#Picture {
width: 120px;
height: 120px;
cursor: pointer;
}
And finally listen for the click on the picture and trigger a click on the input control 最后聆听图片上的点击并触发输入控件上的点击
$("#Picture").click(function () {
$("#imgInp").click();
});
http://jsfiddle.net/LvsYc/3137/ http://jsfiddle.net/LvsYc/3137/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.