更新时间:2023-11-05 19:21:16
是因为在javascript中window.onload事件是唯一的,无论你写多少个都只执行一个,而且是最后一个,
再说你页面不是引了jquery的吗?你完全可以把第一个script改成jq的写法呀。因为$(function(){})可以出现多次,并且都会运行。
window.onload=function typing (){
var oBody=document.getElementById('bg_body');
var oS=oBody.style;
function BgChenge(){
oS.backgroundImage='url(image/'+(parseInt(Math.random() * 6) + 1)+'.jpg)';
};
function BgPosition(){
oS.backgroundRepeat='no-repeat';
oS.backgroundPosition='center';
oS.backgroundAttachment='fixed';
};
function LoadMethod(){
BgChenge();
BgPosition()
};
setInterval(LoadMethod,1000);
};
改写:
<script type="text/javascript">
$(document).ready(function(){
typing();
});
//当然 你也可以把下面这个函数 都改成 jquery的写法
function typing (){
var oBody=document.getElementById('bg_body');
var oS=oBody.style;
function BgChenge(){
oS.backgroundImage='url(image/'+(parseInt(Math.random() * 6) + 1)+'.jpg)';
};
function BgPosition(){
oS.backgroundRepeat='no-repeat';
oS.backgroundPosition='center';
oS.backgroundAttachment='fixed';
};
function LoadMethod(){
BgChenge();
BgPosition()
};
setInterval(LoadMethod,1000);
};
</script>