javascript设置图片大小?

来源:百度知道 编辑:UC知道 时间:2024/06/08 06:39:01
各位大侠:见如下代码:
<html>

<head>

<title></title>
<script type="text/javascript" language="javascript">
window.onload=function()
{
var img= document.getElementsByTagName('img');
for(var i=0;i<img.length;i++)
{
img[i].onmouseover=function()
{
check(this);
}
}
}
function check(img)
{
var imge=new Image();
imge.src=img.src;
alert(imge.width);
imge.width=300;
}

</script>
</head>
<body>
<div>
<ul>
<li><img src="1.gif" /></li>
<li><img src="2.gif" /></li>
<li><img src="3.gif" /></li>
<li><img src="4.gif" /></li>
</ul>
&

你不应该new一个image,你设置的只是这个new的、没显示的对象的大小,下面这样就对:

function check(img)
{
alert(img.width);
img.width=300;
}

刚才在你另外一个帖子里面我回了一个;
你可以通过css来控制。更简单啊~只要替换当前img id的class
例如:
<img src="xxxx.gif" id="img_1" />
好像有个setclassname是吧。

imge.width=300;

==》

imge.style.width=300+"px";

function check(img)
{
var imge=new Image();
imge.src=img.src;
alert(imge.width);
imge.width=300;
}
改写成
function check(img)
{
img.style.width=300;
}