![](https://i-blog.csdnimg.cn/blog_migrate/43f9446ede0edca5a84f34133c055739.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a9f557b5151047b2e3e2bb6c9f40a3b3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/faacead7ecb6c2c1468a3601870134a8.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a1140d2d1942f6f473650b146454b96f.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d2285dc9e22337586585e794d5a0c016.gif)
中间也学到了一点:不要使用children而是使用childNodes来获得子节点。这样才符合w3c的规范,对FF才有比较好的支持。
不多说了,直接看代码吧。
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
< meta name ="Keywords" content ="YES!B/S!" />
< meta name ="Description" content ="This page is from http://Justinyoung.cnblogs.com" />
< title > CSS/Javascript demo </ title >
< style type ="text/css" >
</ style >
</ head >
< body >
< div id ="divTest" >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
</ div >
</ div >
< div id ="divShow" > showhere </ div >
< script type ="text/javascript" language ="javascript" >
// 替换随机图片
var alist = document.getElementsByTagName( " a " );
var acount = alist.length;
var number;
var divShow = document.getElementById( " divShow " );
var maxCount = 0 ;
for (i = 0 ;i < acount;i ++ ){
if (alist[i].title == " Day link " )
{
number = parseInt( 15 * Math.random());
linString = " http://images.cnblogs.com/cnblogs_com/justinyoung/titleICO/ " + number + " .gif " ;
divShow.innerText = divShow.innerText + linString + " ___ " ;
alist[i].childNodes[ 0 ].src = linString; // 采用childNodes而不是children是为了兼容FF。采用w3c的方法。
maxCount = maxCount + 1 ;
if (maxCount > 14 ){
break ;
}
}
}
</ script >
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
< meta name ="Keywords" content ="YES!B/S!" />
< meta name ="Description" content ="This page is from http://Justinyoung.cnblogs.com" />
< title > CSS/Javascript demo </ title >
< style type ="text/css" >
</ style >
</ head >
< body >
< div id ="divTest" >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
< a Title ="Day link" href ="#" >< img src ="titleICO_1.gif" ></ a >
</ div >
</ div >
< div id ="divShow" > showhere </ div >
< script type ="text/javascript" language ="javascript" >
// 替换随机图片
var alist = document.getElementsByTagName( " a " );
var acount = alist.length;
var number;
var divShow = document.getElementById( " divShow " );
var maxCount = 0 ;
for (i = 0 ;i < acount;i ++ ){
if (alist[i].title == " Day link " )
{
number = parseInt( 15 * Math.random());
linString = " http://images.cnblogs.com/cnblogs_com/justinyoung/titleICO/ " + number + " .gif " ;
divShow.innerText = divShow.innerText + linString + " ___ " ;
alist[i].childNodes[ 0 ].src = linString; // 采用childNodes而不是children是为了兼容FF。采用w3c的方法。
maxCount = maxCount + 1 ;
if (maxCount > 14 ){
break ;
}
}
}
</ script >
</ body >
</ html >
keyword:javascript childNodes,javascript children,firefox children,javascript兼容性