下面是代码(在IE6和Firefox2下测试通过):
function test()
{
var retVal = new Array();
var elements = document.getElementsByTagName("A");
//alert('A has '+elements.lenght+'elements');
for (var i=0;i<elements.length;i++)
{
if (elements[i].className=='postTitle2')
retVal.push(elements[i]);
}
//alert('retVal.length='+retVal.length);
if (retVal.length==0) return;
var a = Math.random()*retVal.length;
var b = Math.round(a);
if (b==0) b=1; if (b>retVal.length-1) b=retVal.length-1;
var sHref = retVal[b];//这个是随机获取的超链接
var sInner = retVal[b].innerHTML;//这个是文字
var sResult = '<a href="'+sHref+'" target="_blank"><font color=red>'+sInner+'</font></a>';
//alert(sResult);
return sResult;
}
{
var retVal = new Array();
var elements = document.getElementsByTagName("A");
//alert('A has '+elements.lenght+'elements');
for (var i=0;i<elements.length;i++)
{
if (elements[i].className=='postTitle2')
retVal.push(elements[i]);
}
//alert('retVal.length='+retVal.length);
if (retVal.length==0) return;
var a = Math.random()*retVal.length;
var b = Math.round(a);
if (b==0) b=1; if (b>retVal.length-1) b=retVal.length-1;
var sHref = retVal[b];//这个是随机获取的超链接
var sInner = retVal[b].innerHTML;//这个是文字
var sResult = '<a href="'+sHref+'" target="_blank"><font color=red>'+sInner+'</font></a>';
//alert(sResult);
return sResult;
}