如果你的主頁中需要隨機顯示圖片或者鏈接banner
你該怎麼實現呢?我們介紹使用Javascript來實現這樣的效果!
下面是一個隨機顯示五條Banner的代碼
你可以自己定義的
<script language=javascript>
<!
var b_num =
var b=new Array();
function init_item(){
this
width =
this
height =
this
src =
this
href =
this
border =
this
alt =
this
under =
this
grade=
this
target=
}
function createarray(ba
itemnum) {
for(var i=
; i<itemnum; i++) { ba[i] = new init_item() }
}
function requestbanner(ba){
sum=
;
for(i=
;i<ba
length;i++)sum+=ba[i]
grade
var slump = Math
random();
var talet = Math
round(slump*(sum
))+
;
i=
;
sum=
;
while(i<ba
length && sum<=talet){sum+=ba[i]
grade;i++;}
b=ba[i
];
visa =
;
if (b
target!=
){
visa +=
<a +b
href+
target=
+b
target+
>
;}
else{
visa +=
<a +b
href+
>
;
}
visa +=
<img src=
+b
src+
height=
+b
height;
visa +=
width=
+b
width+
border=
+b
border+
alt=
+b
alt+
>
;
visa +=
</a>
;
document
write(visa);
}
createarray(b
b_num)
b
b[
]
src =
_
/
/
gif
// Banner圖片路徑
b[
]
href =
// Banner鏈接地址
b[
]
target=
_blank
b[
]
width =
b[
]
height =
b[
]
border =
b[
]
alt =
::::網頁教學網::::
// Banner文字提示
b[
]
under =
b[
]
grade =
b
b[
]
src =
_
/
/
gif
b[
]
href =
b[
]
target=
_blank
b[
]
width =
b[
]
height =
b[
]
border =
b[
]
alt =
::::備用域名::::
b[
]
under =
b[
]
grade =
b
b[
]
src =
_
/
/
gif
b[
]
href =
b[
]
target=
_blank
b[
]
width =
b[
]
height =
b[
]
border =
b[
]
alt =
::::技術論壇::::
b[
]
under =
b[
]
grade =
requestbanner(b);
//
>
</Script>
將這段JS代碼插入到<body>相應位置
用戶可修改相關代碼
或者增加
減少Banner的數量
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19408.html