日期:2014-05-16  浏览次数:20372 次

问个简单问题:for循环中连接字符串
for(i=0;i <5;i++){
var   htmlStr   =   i   +   ', ';
}
我想获得最后的结果是:
htmlStr   =   '0,1,2,3,4 ';
如何实现啊?

------解决方案--------------------
for(i=0;i <5;i++){
var htmlStr= " ";
if(i==0)
{htmlStr=htmlStr+i;}
else
{htmlStr=htmlStr + ', '+ i;}
}

------解决方案--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " ">
</HEAD>

<BODY>
<script language= "JavaScript1.2 ">
<!--
var Str= " ";
for(i=0;i <5;i++){
if(i==0)
{Str=Str+i;}
else
{Str=Str + ', '+ i;}
}
alert(Str);
//-->
</script>

</BODY>
</HTML>

------解决方案--------------------
var a =[];
for(var i=0;i <5;i++)
a.push(i);
alert(a.join( ", "));