如何用java控制img对象的高度

来源:百度知道 编辑:UC知道 时间:2024/07/04 09:04:29
例:<img src="/aa.jpg" width=300 id=image_h>
当aa.jpg的高度的超过150的时候,如何把它的高度设定为100呢?
(注:因为根据程序需要,所以不能预先就就它的高度值给设好,以免图片变形嘛),大家帮帮忙看看啊

width是宽度,height才是高度,反正顺著改就好了
<script language="JavaScript">
function display(image)
{
if(image.width>150)
return 100;
else
return image.width;
}
</script>
<img src="/aa.jpg" width=display(this) id=image_h>
不懂楼上为什麽要搞得这麽复杂

<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
Im