document.images——顾名思义,文档中的图象资源。
若多个图片,按照显示的先后采用数组接受,先从这里看起
<script language=javascript>
function cnbruce(i){
alert(document.images[i].src)
}
</script>
<img src="http://blog.blueidea.com/button/88x31.gif">
<img src="http://www.cnbruce.com/images/logo.gif"><br>
<input onclick="javascript:cnbruce(0)" value="图片1地址" type=button>
<input onclick="javascript:cnbruce(1)" value="图片1地址" type=button>
程序应该没多大问题,其中关于参数i,我每次都需要赋值。现在用document.images.length来判断个数
<script language=javascript>
function cnbruce(){
alert("本页有 "+document.images.length+" 张图片")
}
</script>
<img src="http://blog.blueidea.com/button/88x31.gif">
<img src="http://www.cnbruce.com/images/logo.gif"><br>
<input onclick="javascript:cnbruce()" value="看看多少张" type=button>
SO,下面就开始显示所有图片的地址
<script language=javascript>
function cnbruce(){
cnjames='';
for (cnrose=0;cnrose<document.images.length;cnrose++)
{
cnjames+=document.images[cnrose].src+"\n";
}
alert(cnjames)
}
</script>
<img src="http://blog.blueidea.com/button/88x31.gif">
<img src="http://www.cnbruce.com/images/logo.gif"><br>
<input onclick="javascript:cnbruce()" value="看看本页所有图片的地址" type=button>
OK,将程序补充完整,就是下面的了,将文档只显示所有的图片
cnbruce='';
for (cnrose=0;cnrose<document.images.length;cnrose++)
{
cnbruce+='<img% src='+document.images[cnrose].src+'><br>'
}
if(cnbruce!='')
{
document.write(cnbruce);
void(document.close())
}
else{
alert('No images!')
}
将该多行代码装换为一行,将空格替换为%20
那么现在得到的这一大整代码粘贴到你当前的URL地址栏中,你会发现什么?:)
javascript:cnbruce='';for%20(cnrose=0;cnrose<document.images.length;cnrose++){cnbruce+='<img%20src='+document.images[cnrose].src+'><br>'};if(cnbruce!=''){document.write(cnbruce);void(document.close())}else{alert('No%20images!')}
over,the end.
转自: http://www.cnbruce.com/blog/showlog.asp?cat_id=5&log_id=664
本文地址:http://com.8s8s.com/it/it30820.htm