请问,这段JS代码怎么用在整个页面?

来源:百度知道 编辑:UC知道 时间:2024/05/14 10:57:48
下面是一段完整的缩略图代码。用法是:<img src="80.jpg" border="0" onload="javascript:DrawImage(this);">

但是怎么调用才能用在整个页面?即:<img src="80.jpg" border="0">
不用后面的onload。。。

<script language="javascript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 164/112){
if(image.width>164){
ImgD.width=164;
ImgD.height=(image.height*164)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>112){
ImgD.height=112;
ImgD.width=(image.width*112)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;

在<head></head>中写一个函数遍历所有的img元素 然后用每个元素为参数调用 DrawImage函数
遍历img的函数如下:
<script type="text/javascript">
function loadimage()
{
var img=document.body.getElementsByTagName("IMG");
for(var i=0;i<spans.length;i++)
{
DrawImage(img[0]);
}
}
</script>
然后在body的Onload事件中调用loadimage函数
<body Onload="loadimage()">
这样就可以了

所略图还要用于整个界面,什么意思?那样的话那就失去了它的意义,难道你想让那样小的所略图覆盖满整个界面吗,那就是有很多个小的所略图来组成了整个界面了?还是想让那张图一大图的形式来覆盖整个界面,那样的话就很简单了