可以让无空格连续的字符换行的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;\"/>";
    }
   }

原文地址:https://www.cnblogs.com/Random/p/889639.html