可以让无空格连续的字符换行的JS(名字好难取-_-!)

可以避免因某一连续字符过长而使页面布局遭破坏。。。

function SetWordNewLine(Words,MaxLength)
   { 
    var WordArray;
    WordArray=Words.split(" ");
    
    for(i=0;i<WordArray.length;i++)
    {
     if(WordArray[i].length>MaxLength)
     {
      WordArray[i]=GetWordSubString(WordArray[i],MaxLength);
     }

    }
    var ResultWord="";
    for(i=0;i<WordArray.length;i++)
    {
     ResultWord+=WordArray[i]+" ";
    }
    return ResultWord;
   }
   
   //递归截取字符串
   function GetWordSubString(_Words,_MaxLength)
   {
    if(_Words.length>_MaxLength)
    {

     return _Words.substring(0,_MaxLength)+"<p style=\"margin-top:0px; margin-bottom:0px;\"/>"+GetWordSubString(_Words.substring(_MaxLength,_Words.length),_MaxLength);
    }
    else
    {
     return _Words+"<p style=\"margin-top:0px; margin-bottom:0px;\"/>";
    }
   }

posted @ 2007-09-11 13:33  随机  阅读(1098)  评论(1编辑  收藏  举报