我想在上传之前显示图像的预览。我找到了适用于 ie6 和 firefox 的部分解决方案,但尚未在 ie7 或 ie8 中对其进行测试。但我想要一个适用于 safari、ie7 和 ie8 的解决方案。下面是结合ie6和firefox方案得到的方案:
function preview(what) {
if(jQuery.browser.msie) {
document.getElementById("preview-photo").src=what.value;
return;
}
else if(jQuery.browser.safari) {
document.getElementById("preview-photo").src=what.value;
return;
}
document.getElementById("preview-photo").src=what.files[0].getAsDataURL();
// alert(jQuery("#preview-photo").height());
// alert(jQuery("#preview-photo").width());
var h = jQuery("#preview-photo").height();
var w = jQuery("#preview-photo").width();//assuming width is 68, and height is floating
if ((h > 68) || (w > 68)){
if (h > w){
jQuery("#preview-photo").css("height", "68px");
jQuery("#preview-photo").css("width", "auto");
}else {
jQuery("#preview-photo").css("width", "68px");
jQuery("#preview-photo").css("height", "auto");
}
}
}
getAsDataURL() 部分在 firefox 中有效,“src=what.value”部分在 ie6 中有效,但在 safari 中有效,“src=what.value”在 ie7 和 ie8 中也有效吗?如果没有,是否有一些解决方案也适用于那里?如果我能让图像预览在 5 或 6 个浏览器中工作,我会很高兴。如果不是,那么唯一的选择是拥有两个表单和另一个表单的图像上传部分?