Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

求帮我看下这个怎么写for循环

var r1 = document.createElement("b");
var r1content=document.createTextNode(filterArr[0]);
r1.appendChild(r1content);
var r01 = document.getElementById("rank1");
r01.appendChild(r1);

var r2 = document.createElement("b");
var r2content=document.createTextNode(filterArr[1]);
r2.appendChild(r2content);
var r02= document.getElementById("rank2");
r02.appendChild(r2);

var r3 = document.createElement("b");
var r3content=document.createTextNode(filterArr[2]);
r3.appendChild(r3content);
var r03 = document.getElementById("rank3");
r03.appendChild(r3);

是这样的,我想写一个for循环,但是不知道怎么设置变量,想写成想把r1,r2,r3;r01,r02,r03;filterArr[0]filterArr[1]filterArr[2]都写成rn的形式,但是for循环里面,for(var i=o;i<6;i++){}这个不会变形,谁能教教我,谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
    for(let i= 0; i< filterArr.length; i++){
        let r = document.createElement("b");
        let rContent = document.createTextNode(filterArr[i]);
        r.appendChild(rContent);
        let rRank = document.getElementById("rank"+i);
        rRank.appendChild(r);
    }

你说的是这个意思吧=。=


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...