asp方法调用

来源:百度知道 编辑:UC知道 时间:2024/05/10 13:06:59
<img src="016.jpg" onload="if((this.width / this.height) > 3/2) this.width='150'; if((this.width / this.height) < 3/2) this.height='100'">
这是我写的图片大小按比例缩放的代码,就是onload里面的代码太多了,可不可以把onload里面的这些改成一个方法来调用啊?请高手支招!

希望改了后成这样:(免得每添加一个图片就要复制很长的onload里面的代码)
<img src="016.jpg" onload="调用方法">
<img src="015.jpg" onload="调用方法">
<img src="014.jpg" onload="调用方法">
<img src="013.jpg" onload="调用方法">

很简单啊,这不代码你都有了嘛,把它做成一个js不就可以了吗?
将这段JS放在<head>与</head>之间:
<script type="text/javascript">
function resizepic(thispic){
if((thispic.width/thispic.height)>3/2) thispic.width='150';
if((thispic.width/thispic.height)<3/2) thispic.height='100';
}
</script>

然后你添加图片的时候只需调用这个JS就可以了。即:
<img src="016.jpg" onload="resizepic(this);">
<img src="015.jpg" onload="resizepic(this);">
<img src="014.jpg" onload="resizepic(this);">
<img src="013.jpg" onload="resizepic(this);">