数组使用while循环显示在项目符号列表中

时间:2018-02-13 00:54:30

标签: while-loop bulletedlist

我是Javascript的新手,我正在尝试使用While循环而不是For循环重新创建以下代码。我可以将它列入清单但不在无序列表中。任何帮助将不胜感激。

var middle = "Wayne";
var streets = ["Elm Street", "Sesame Street", "Coronation Street", "Jump 
Street", "Wall Street"];
var sLen = streets.length;
var text = "<ul>";
for (i = 0; i < sLen; i++){
text += "<li>" + middle + " " + streets[i] + "</li>";
}
text += "</ul>";
document.getElementById("nameList").innerHTML = text;

2 个答案:

答案 0 :(得分:0)

var i = 0;
while(i<sLen){
   ...execution code goes here...  
   i++;
}

请查看此参考,详细了解while循环while-loops-Javascript

答案 1 :(得分:0)

var middle = "Wayne";  
var streets = ["Elm Street", "Sesame Street", "Coronation Street", "Jump 
Street", "Wall Street"];
var sLen = streets.length;
var text = "<ul>";

while(sLen--) {
    text += "<li>" + middle + " " + streets[sLen] + "</li>";
}

text += "</ul>";
document.getElementById("nameList").innerHTML = text;

我希望它会对你有所帮助。