function left(mainStr,lngLen) { 
if (lngLen>0) {return mainStr.substring(0,lngLen)} 
else{return null} 
} 以上是程序,现在不论是中文还是英文或字母,全不算1个字,怎么改才能实现英文或字母算1个字,中文算2个字?

解决方案 »

  1.   

        function gettextBybyte(str,len){
            var count=0;
            var rtn=""
            for(i=0;count<len && count<str.length;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
            return str.substring(0,i);
        }
        alert(gettextBybyte("所在2部门",5));
      

  2.   

    function gettextBybyte(str,len){
      var count=0;
      var rtn=""
      for(i=0;count<len && count<str.length;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
      return str.substring(0,i);
    }
    function history_show()
    {   
     var history_info=hgetCookie("history_info");
     if(history_info!=null){
     document.getElementById("historyurl").innerHTML="您的浏览记录:";
     var history_arg=history_info.split("_splitchar_");
     for(i=0;i<displayamount;i++){
      if(history_arg[i]){
       var wlink=history_arg[i].split("+");
       if(wlink[0]!="null"&&wlink[0]!="underfind"){document.getElementById("hisurl"+i).innerHTML=("<a href='"+wlink[1]+"' target='_blank'>"+gettextBybyte(wlink[0],14)+"</a>");}
      }
      }
    }
    else{
      document.getElementById("historyurl").innerHTML="您的浏览记录:暂时没有任何浏览记录";
    }
    }一运行,就死循环了,好半天才能恢复过来。以前没加gettextBybyte(wlink[0],14)这句没事。
      

  3.   

    用charAt 返回的值可以判断是否是英文数字 对应的ascii码值