Skip to content

Instantly share code, notes, and snippets.

@xufan6
Created February 20, 2012 04:46
Show Gist options
  • Save xufan6/1867900 to your computer and use it in GitHub Desktop.
Save xufan6/1867900 to your computer and use it in GitHub Desktop.
全角转半角的js
<html>
<textarea name="n" type="text" id="n" onkeyup="CtoH(this);" style="width:100%;height:100%;"></textarea>
<SCRIPT>
function CtoH(obj){
var str=obj.value;
var result="";
for (var i = 0; i < str.length; i++){
if (str.charCodeAt(i)==12288){
result+= String.fromCharCode(str.charCodeAt(i)-12256);
continue;
}
if (str.charCodeAt(i)!=65292 && str.charCodeAt(i)>65280 && str.charCodeAt(i)<65375) result+= String.fromCharCode(str.charCodeAt(i)-65248);
else result+= String.fromCharCode(str.charCodeAt(i));
}
obj.value=result;
}
</SCRIPT>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment