js 截取10个字节

 
 
function Count(text, long) {
  var strlen="";
  var str = document.getElementById('TextBox1').value;
  var realLength = 0, len = str.length, charCode = -1;
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i);
    if (charCode >= 0 && charCode <= 128)
      realLength += 1;
    else
      realLength += 2;
  }
  var maxlength = new Number(long); // Change number to your max length.
  if (realLength > maxlength) {
    for (var i = 0; i <len; i++) {
      strleg=str.substring(0,i);
      for(var j=0;j<=10;j++)
      {
        charCode1 = strleg.charCodeAt(j);
        if (charCode1 >= 0 && charCode1 <= 128)
          j += 1;
        else
          j += 2;
      }
      if(j==Number(10))break;
    }
    text.value =strleg; //text.value.substring(0, maxlength);
    alert(maxlength);
  }
}
原文地址:https://www.cnblogs.com/zhilu/p/14358058.html