开发者社区> 问答> 正文

javascript怎样在一个页面中加三个js

window.onload = rolloverInit;
function rolloverInit() {
for (var i=0; i<document.links.length; i++) 
var linkObj = document.links[i];
if (linkObj.className) {
var imgObj = document.getElementById(linkObj.className);
if (imgObj) {
setupRollover(linkObj,imgObj);
}
}
}
}
function setupRollover(thisLink,textImage) {
thisLink.imgToChange = new Array;
thisLink.outImage = new Array;
thisLink.overImage = new Array;
thisLink.imgToChange[0] = textImage;
thisLink.onmouseout = rollOut;
thisLink.onmouseover = rollOver;    
thisLink.outImage[0] = new Image();
thisLink.outImage[0].src = textImage.src;



thisLink.overImage[0] = new Image();
thisLink.overImage[0].src = "images/" + thisLink.id + "Text.gif";



var rolloverObj = document.getElementById(thisLink.id + "Img");
if (rolloverObj) {

    thisLink.imgToChange[1] = rolloverObj;


    thisLink.outImage[1] = new Image();
    thisLink.outImage[1].src = rolloverObj.src;


    thisLink.overImage[1] = new Image();
    thisLink.overImage[1].src = "images/" + thisLink.id + "_on.gif";

} 
}
function rollOver() {
for (var i=0;i<this.imgToChange.length; i++) {
this.imgToChange[i].src = this.overImage[i].src;
}
}
function rollOut() {
for (var i=0;i<this.imgToChange.length; i++) {
this.imgToChange[i].src = this.outImage[i].src;
}
}
window.onload = rotate;
var thisAd = 0;
function rotate() {
var adImages = new Array("a38.png","a37.jpg","a36.png");
thisAd++;
if (thisAd == adImages.length) {
thisAd = 0;
}
document.getElementById("adBanner").src = adImages[thisAd];
setTimeout(rotate, 2 * 1000);
}
window.onload = initAll;
function initAll(){
document.getElementById("过去").onclick=saysomething;
document.getElementById("现在").onclick=saysomething;
document.getElementById("未来").onclick=saysomething;
}
function saysomething(){
switch(this.id){
case "过去":
alert("过去就是一首诗,思念的美");
break;
case "现在":
alert("现在就是一曲歌,旋律的荡漾");
break;
case "未来":
alert("未来就是一道桥,神秘且向往");
break;
default:
} 
}

展开
收起
小旋风柴进 2016-03-18 10:01:06 1985 0
1 条回答
写回答
取消 提交回答
  • 直接不是回过你了,要用attachEvent或者addEventListener,不能直接window.onload=xxxx来添加事件

    
    
     function addEvent(o,evt,func){
      if(o.attachEvent)o.attachEvent('on'+evt,func);
        else if(o.addEventListener)o.addEventListener(evt,func,false);
    }
    //window.onload = rolloverInit;
     addEvent(window,'load',rolloverInit)
    
    //window.onload = initAll; 
     addEvent(window,'load',initAll)
    
    //window.onload = rotate;
     addEvent(window,'load',rotate)
    2019-07-17 19:05:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript函数 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载