js 页面刷新同时更换图片文字与连接
//JavaScriptDocumentdocument.write('<liclass="col"style="margin:0;padding:0;"><aid="x...
// JavaScript Document
document.write('<li class="col" style="margin:0; padding:0;"><a id="xurl" title="小说" target="_self" href=""><img id="xtu" src="" /><h4 id="xword" style="color:red;"></h4></a></li>');
var arr=new Array("11","22","33","44","55","66");
var arr1=new Array("11","22","33","44","55","66");
var arr2=new Array("11","22","33","44","55","66");
var xtu=document.getElementById("xtu");
var xurl=document.getElementById("xurl");
var xword=document.getElementById("xword");
/*var curIndex=0;
var curIndex1=0;
var curIndex2=0;*/
window.onload=function(){
var xword1=parseInt(Math.random()*arr2.length);
xword.innerHTML=arr2[xword1];
var xurl1=parseInt(Math.random()*arr1.length);
xurl.href=arr1[xurl1];
var xtu1=parseInt(Math.random()*arr.length);
xtu.src=arr[xtu1];
}
我这里随机有了。但是不图片,文字,链接不同步,希望哪位大神帮忙给个完整的。谢谢! 展开
document.write('<li class="col" style="margin:0; padding:0;"><a id="xurl" title="小说" target="_self" href=""><img id="xtu" src="" /><h4 id="xword" style="color:red;"></h4></a></li>');
var arr=new Array("11","22","33","44","55","66");
var arr1=new Array("11","22","33","44","55","66");
var arr2=new Array("11","22","33","44","55","66");
var xtu=document.getElementById("xtu");
var xurl=document.getElementById("xurl");
var xword=document.getElementById("xword");
/*var curIndex=0;
var curIndex1=0;
var curIndex2=0;*/
window.onload=function(){
var xword1=parseInt(Math.random()*arr2.length);
xword.innerHTML=arr2[xword1];
var xurl1=parseInt(Math.random()*arr1.length);
xurl.href=arr1[xurl1];
var xtu1=parseInt(Math.random()*arr.length);
xtu.src=arr[xtu1];
}
我这里随机有了。但是不图片,文字,链接不同步,希望哪位大神帮忙给个完整的。谢谢! 展开
1个回答
展开全部
var xtu=document.getElementById("xtu");
var xurl=document.getElementById("xurl");
var xword=document.getElementById("xword");
这三行要放到window.onload事件里面去,否则的话在页面还没有加载完毕就运行是无法获取这些Dom元素的。
var xurl=document.getElementById("xurl");
var xword=document.getElementById("xword");
这三行要放到window.onload事件里面去,否则的话在页面还没有加载完毕就运行是无法获取这些Dom元素的。
追问
我的意思是连接,图片,文字同步。页面刷新的时候同步更换下标。链接是:11的时候,图片也是11;文字也是:11;链接是:22的时候,图片也是22;文字也是:22;这个怎么处理。。希望给个完整的代码。我不知道怎么写!谢谢
追答
document.write('<li class="col" style="margin:0; padding:0;"><a id="xurl" title="小说" target="_self" href=""><img id="xtu" src="" /><h4 id="xword" style="color:red;"></h4></a></li>');
var arr=new Array("11","22","33","44","55","66");
var arr1=new Array("11","22","33","44","55","66");
var arr2=new Array("11","22","33","44","55","66");
/*var curIndex=0;
var curIndex1=0;
var curIndex2=0;*/
window.onload=function(){
var xtu=document.getElementById("xtu");
var xurl=document.getElementById("xurl");
var xword=document.getElementById("xword");
var x=Math.random(); /*---看这行!---*/
var xword1=parseInt(x*arr2.length);
xword.innerHTML=arr2[xword1];
var xurl1=parseInt(x*arr1.length);
xurl.href=arr1[xurl1];
var xtu1=parseInt(x*arr.length);
xtu.src=arr[xtu1];
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询