我目前在我的网站中使用 JS 旋转横幅。横幅出现在所有浏览器中,大多数版本的 IE 除外。
var banners = Array(); //The first element pushed in the array is at the index 0
banners.push({link: '', target:'_blank', image: '', title: ''});
var rotate = function() {
//Find all images with class myImage
var images = document.getElementsByClassName('myImage');
var total = images.length;
//Looping through all the elements found
for (var i = 0; i < total; i++) {
if (banners.length == 0) {
break;//No more banners can't display anymore
}
//Retrieve a random banner
var rnd = Math.floor((Math.random()*banners.length));
//Add the html to the element
images[i].innerHTML = '<a href="'+banners[rnd].link+'" target="'+banners[rnd].target+'"><img src="'+banners[rnd].image+'" title="'+banners[rnd].title+'" /></a>'; //Added target blank and empty alt attribute
banners.splice(rnd, 1);
}
}
if (window.addEventListener) // W3C DOM
window.addEventListener('load',rotate,false);
else if (window.attachEvent) // IE DOM
window.attachEvent("onload", rotate);
然后我在我的 html 中使用一个简单的 div 调用横幅。
< div class="myImage">< /div>
任何帮助都会非常感激。
谢谢