在线等:为何图片载入时缩放失效?

mqmelon 2008-09-12 07:19:58
有一个显示产品的页面,产品中有图片,本意是想图片大于一定尺寸自动缩小,但是下面这段代码,只有加入alert("hi");之后才会有效果,一旦去掉这句就不缩放,为什么呢?请高手帮帮我,在线等,急用.
<img id="ProImg" border=0 src="UploadFiles/2008911175547745.jpg" onload="javascript:zoomIt(this);">
<script language="Javascript">
function zoomIt(theImg)
{
alert("hi");
if (theImg.width>18){
theImg.width=18;
}

if(theImg.height>20){
theImg.height=20;
}

}
</script>

原来用的代码为:<img id="ProImg" border=0 src="UploadFiles/2008911175547745.jpg" onload="javascript:if(this.width>20)this.width=20;">
但是发现一样问题存在,只有在语句中加入alert("hi");之后才会实现缩放.
在线等!
...全文
183 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mqmelon 2008-10-11
  • 打赏
  • 举报
回复
这个问题一直都没有解决,但是不管如何,还是谢谢各位热心了.
ps:最近一直在看asp.net所以没有上来,拖了很久了,真是抱歉.
「已注销」 2008-09-22
  • 打赏
  • 举报
回复
楼上的答案是不对的。

你这情况挺复杂。是用script异步载入的……。有时候我在页面添加google广告,也遇到有的可以,有的不可以的情况。
quxiuer 2008-09-22
  • 打赏
  • 举报
回复
因为你的图片load需要一段时间

<img id="ProImg" border=0 src="UploadFiles/2008911175547745.jpg" onload="javascript:zoomIt(this);">

<script language="Javascript">
function zoomIt(theImg) {
setTimeout('a=changeSrc(theImg)',200);}

function zoomIt(theImg) {
if (theImg.width>18) {
theImg.width=18;
}

if(theImg.height>20) {
theImg.height=20;
}
}
</script>



mqmelon 2008-09-15
  • 打赏
  • 举报
回复
以下是出问题的页面,经过测试,我找到了影响这个结果的代码,但是我却无法获知是为什么?
<table width="878" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="2"></td>
<td width="180" height="179" valign="top" class="TableBorder">
<table width="100%" align="center" border="0" cellspacing="1" cellpadding="0">
<tr><td height="34" class="indexTitleRight">产品列表</td></tr>
<tr><td height="10">
<div align="right"><% =WebMenu("ProductSort",0)%></div>
</td></tr>
</table>
</td>
<td width="10"></td>
<td valign="top" align="center">
<div>
<table width="490" border="0" cellspacing="0" cellpadding="0">
<TR><TD height="1" colspan="3">
<!--广告位-->
<div class="adDiv"><script src="getAd.asp?Type=详细&Type1=产品&AdW=490&AdH=80&Ads=1"></script></div>
</td></tr>
</table>
<!--产品内容-->
<table width="490" border="0" class="TableTriBorder" align="center" cellpadding="5" cellspacing="0" >
<TR>
<TD class=BgRigBotBorder vAlign=center align=middle width=170 rowSpan=9>
<div><img src="<%=rs("DefaultPicUrl")%>" onload="Javascript:javascript:if(this.width>10)this.width=10;"></div>

</TD>
<td width="60" align="right" class="BgRigBotBorder">产品编号</td>
<td class="botBorder"><%=rs("Product_Id")%> </td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">产品名称</td>
<td class="botBorder"><%=rs("title")%> </td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">所属类别</td>
<td class="botBorder"><%=rs("ClassName")%> </td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">产品售价</td>
<td class="botBorder">
<%response.write "直销价格:<font color=#0000ff>¥" & rs("NormalPrice") &"</font> 元<br>"%>
</td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">发布日期</td>
<td class="botBorder"><%=rs("AddTime")%> </td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">产品规格</td>
<td class="botBorder"><%=rs("Spec")%> </td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">购买次数</td>
<td class="botBorder"><%=rs("BuyHits")%> 次</td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">点击次数</td>
<td class="botBorder"><%=rs("Hits")%> 次</td>
</tr>
<tr>
<td width="60" align="right" class="BgRigBotBorder">马上购买</td>
<td class="botBorder"><a href="add.asp?product_Id=<%=rs("ID")%>&ProductNum=1" target="_blank"><img src="images/cart.gif"></a></td>
</tr>
</table>
<table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
<tr><td height="5"></td></tr>
<tr><td class="indexTitleRight">产品详细参数</td></tr>
<tr><td class="NewsContent"><%=rs("content")%></td></tr>
</table>
</td>
<td width="10"></td>
<td width="200" valign="top">
<!-----此处为影响结果正确的部分------>
<table width="98%" align="right" border="0" cellspacing="1" cellpadding="0">
<tr><td class="indexDiv180">产品购买排行</td></tr>
<tr><td style="height:183px;"><script src="getProduct_top.asp?SA=YES&stime=yes&TLen=12&Num=10"></script></td></tr>
<tr><td style="padding-top:10px;text-align:center;"><script src="getAd.asp?Type=详细&Type1=产品&AdW=170&AdH=150&Ads=2"></script></td></tr>
</table>
<!-----但是文件上部分也有类似的调用,但是不影响结果,而且经过检查认为此部分的代码也没什么问题------>
</td>
</tr>
</table>
mqmelon 2008-09-15
  • 打赏
  • 举报
回复
其实本来这段代码就是应该没有问题的,但是偏偏在这个站出了问题,同一段代码我的留言本在本机上也工作正常,但是这个站(留言本是另一个站的)就是不工作.我实在找不出什么原因了.我查过文件头,好象不是这个原因.
mqmelon 2008-09-15
  • 打赏
  • 举报
回复
3楼的代码还是过不了.其实对于这行代码我一直很困惹,因为在当年我写的一个留言本中用的也是这行代码,记得当时是可以的,但是如今怎么也不行,我在台式机和本本上通试过,就是不工作,但是只要加上一句可以暂停的语句立即工作,这是为何?
4楼的代码我测试没通过.
cloudgamer 2008-09-12
  • 打赏
  • 举报
回复
好像可以啊
最好给个测试页
或者试试
setTimeout(function(){
if (theImg.width>18){
theImg.width=18;
}

if(theImg.height>20){
theImg.height=20;
}

},10)
sd5816690 2008-09-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mqmelon 的回复:]
没人知道吗?
[/Quote]
都过中秋去了。。。

改了一下:
<img id="ProImg" border=0 src="http://i3.sinaimg.cn/home/deco/2008/0325/sinahome_Logo.gif" onload="javascript:if(this.offsetWidth > 20)this.style.width='20px';">

通过了 IE6 和 FF
mqmelon 2008-09-12
  • 打赏
  • 举报
回复
没人知道吗?
mqmelon 2008-09-12
  • 打赏
  • 举报
回复
不要沉呀,顶上来。

87,910

社区成员

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

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