如果简单地问,就是如何取得img的宽度和高度?

NorthMan 2003-07-31 03:31:10
您好:
我做个网页,是从本地选择图片,然后点击“显示”,把img的src属性连接到本地文件。这一步我实现了,但是怎么把显示的图片的高度和宽度在text中表示出来呢?

--------------------------------------------
部分网页代码。。。
</tr>
<tr align="center">
<td colspan="2"><img name="picView" ></td>
</tr>
---------------------------------------------
在scritp 中写:
document.forms[0].picView.src=document.forms[0].file.value;
document.forms[0].txtWidth=document.forms[0].picView.width;
document.forms[0].txtHeight=document.forms[0].picView.Height;
其中第一句能正确执行,在网页上显示出图片,由于没有指定图片宽度和高度,所以图片是原始的高度和宽度。我希望取得这个值,该如何去做?

上面的脚本中,第二句,第三句执行时,IE提示错误:对象不支持此属性或方法。
...全文
205 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
meizz 2003-08-15
  • 打赏
  • 举报
回复
<input type=file id=meizz onpropertychange="mm(this.value)">

<SCRIPT LANGUAGE="JavaScript"><!--
function mm(s)
{
var img = new Image();
img.src = s;
alert("高 = "+ img.height + "\n宽 = "+ img.width);
alert("fileSize = "+ img.fileSize +" 字节");
}
//--></SCRIPT>
  • 打赏
  • 举报
回复
to kypfos(kypfos):

this.fileSize/1024

kypfos(kypfos)
我想保留小数点2位小数,该怎么写?
round(kypfos(kypfos),2)怎么不可以呢?缺少对象错误!
awaysrain 2003-07-31
  • 打赏
  • 举报
回复
加入<img id=img1 ..>时不要设置宽高,然后img1.clientHeight;img1.clientWidth;
meizz 2003-07-31
  • 打赏
  • 举报
回复
agree kypfos(kypfos)

<img src=http://expert.csdn.net/images/csdn.gif border=0
onload="alert('filesize : '+ fileSize +' Byte\r\nwidth : '+ clientWidth + '\r\nheight : '+ clientHeight)">
Yanbin_Q 2003-07-31
  • 打赏
  • 举报
回复
<img src=http://expert.csdn.net/images/csdn.gif onclick=alert(this.fileSize/1024+"KB")>


<img src=http://expert.csdn.net/images/csdn.gif border=0

onload="alert('width : '+ this.clientWidth + '\r\nheight : '+ this.clientHeight)">


saucer 2003-07-31
  • 打赏
  • 举报
回复
do you mean document.forms[0].txtWidth.value and document.forms[0].txtHeight.value???


<form>
<img name=picView>
<input type=file name=file>
<input name=txtWidth>
<input name=txtHeight>
<input type=button onclick="GetDimension()" value=get>
</form>

<script language="javascript">
function GetDimension()
{
if (document.forms[0].file.value == "")
return;

document.forms[0].picView.src= document.forms[0].file.value;
document.forms[0].picView.onload = function() {
document.forms[0].txtWidth.value=this.width;
document.forms[0].txtHeight.value=this.height;
}
}
</script>
随风奔跑 2003-07-31
  • 打赏
  • 举报
回复
用fso是一个得到文件大小的方法。
不知道大侠们取得文件大小更好的办法是什么?
  • 打赏
  • 举报
回复
图片的大小怎么得到呢?就是有多少字节(B)请教。
  • 打赏
  • 举报
回复
NorthMan (青铜圣斗士:黑暗天龙) 可以把整页代码贴出来吗?
形印声传 2003-07-31
  • 打赏
  • 举报
回复
agree with 思归~~
shyslysky 2003-07-31
  • 打赏
  • 举报
回复
document.forms[0].picView.src=document.forms[0].file.value;
document.forms[0].txtWidth=document.forms[0].picView.width;
document.forms[0].txtHeight=document.forms[0].picView.Height;

应该是:
document.forms[0].txtWidth.value=document.forms[0].picView.width;
document.forms[0].txtHeight.value=document.forms[0].picView.Height;

87,987

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧