js切り取り、中国語対応
2278 ワード
<br><br><br> var GetLength = function (str) {<br> ///<summary> , 2, 1</summary><br> ///<param name="str"> </param><br> var realLength = 0, len = str.length, charCode = -1;<br> for (var i = 0; i < len; i++) {<br> charCode = str.charCodeAt(i);<br> if (charCode >= 0 && charCode <= 128) realLength += 1;<br> else realLength += 2;<br> }<br> return realLength;<br> };<br><br><br> //js , <br> // , , 。 <br> // , <br><br> /** <br> * js , <br> * @param str: <br> * @param len: <br> */<br> function cutstr(str, len) {<br> var str_length = 0;<br> var str_len = 0;<br> str_cut = new String();<br> str_len = str.length;<br> for (var i = 0; i < str_len; i++) {<br> a = str.charAt(i);<br> str_length++;<br> if (escape(a).length > 4) {<br> // 4 <br> str_length++;<br> }<br> str_cut = str_cut.concat(a);<br> if (str_length >= len) {<br> str_cut = str_cut.concat("...");<br> return str_cut;<br> }<br> }<br> // , ; <br> if (str_length < len) {<br> return str;<br> }<br> }<br>$(function () { <br> $("input[name=username]").bind('keyup', function () {<br> if (GetLength($(this).val()) > 10) { <br> $(this).val(cutstr($(this).val(), 10)); <br> return; <br> } <br> }); <br>}); <br>