<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>自动限制图片大小</title>
<script language="javascript">
function fixImageSize(src, width, height){
var n=setInterval("setFixSize(" + src.uniqueID + ")", 100);
src.setAttribute("fixhandler", n);
src.οnerrοr=function(){
clearInterval(n);
src.setAttribute("fixhandler");
}
}
function setFixSize(o){
var w=o.width;
var h=o.height;
if (!w*h) return;
var W=100; var H=100 //如果要统一尺寸,则把下面两行换成当前这一行,并且无需在IMG标签中设置 fixWidth, fixHeigh 属性
/*var W=o.getAttribute("fixwidth");
var H=o.getAttribute("fixheight");*/
if (w>W){
h=h * (W/w);
w=W;
}
if (h>H){
w=w * (H/h);
h=H;
}
o.width=w;
o.height=h;
var n=o.getAttribute("fixhandler");
clearInterval(n);
o.setAttribute ("fixhandler", 0);
}
function autoSetImageSize(o){
var a=arguments;
if (a.length==0) a=[document.body];
for (var i=0; i<document.images.length; i++)
for (var j=0; j<a.length; j++)
if (isAncestor(document.images[i], a[j])) setFixSize(document.images[i]);
}
function isAncestor(child, ancestor){
if (child && child.parentElement){
if (child.parentElement==ancestor) return true;
else return isAncestor(child.parentElement, ancestor);
}
return false;
}
</script>
</head>
<body οnlοad="autoSetImageSize(ok, yes, no);">
<div id="ok">
<img src="ok.jpg">
</div>
<div id="no">
<img src="no.jpg">
</div>
<div id="yes">
<img src="yes.jpg">
</div>
<div id="yeah">
<img src="yeah.jpg">
</div>
</body>
</html>