<!--html代码-->
<div style="position: relative;width: 370px;">
<input type='text' name='textfield' id='textfield' class='txt' style="color:red;" value="图片规格为300*100" onfocus="this.value=''" onblur="if(this.value==''){this.value='图片规格为300*100'}" />
<input type='button' class='btn' value='浏览...' />
<input type="file" name="largeImg" class="file" id="fileField"
size="28"onchange="chartPicture(this.id)" />
</div>
<!--js代码-->
<script type="text/javascript">
//上传图片之前判断上传的图片是否符合要求
function chartPicture(fileObj){
var img=new Image();
var value = document.getElementById(fileObj);
if (value.files && value.files[0]) {
//火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式
img.src = window.URL
.createObjectURL(value.files[0]);
} else if(document.selection){
//IE下,使用滤镜
value.select();
var imgSrc = document.selection.createRange().text;
img.src=imgSrc;
}
//因为这里只有把图片添加到页面后才可获取其高和宽
document.body.appendChild(img);
img.onload = function(){
alert(img.offsetWidth);
img.style.display = 'none';
};
}
</script>
这种方法有其缺点,那就是在ie下有时有点不好使