87,909
社区成员
发帖
与我相关
我的任务
分享
str = DBC2SBC(str, true);
function DBC2SBC(str, flag){
var result = '';
str = str.replace(/。/g,".");
for(var i=0;i<str.length;i++){
code = str.charCodeAt(i);
if(flag){
if(code >= 65281 && code <= 65373) result += String.fromCharCode(str.charCodeAt(i) - 65248);
else if(code == 12288) result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
else result += str.charAt(i);
}
else{
if(code >= 33 && code <= 126) result += String.fromCharCode(str.charCodeAt(i) + 65248);
else if(code == 32) result += String.fromCharCode(str.charCodeAt(i) - 32 + 12288);
else result += str.charAt(i);
}
}
return result;
}
<input
onkeypress="var k=event.keyCode; return k>=48&&k<=57"
onpaste="return !clipboardData.getData('text').match(/\D/)"
ondragenter="return false" style="ime-mode:Disabled"
>
<input type="text" onchange="this.value = this.value.replace(/[0123456789]/g, function(v){return v.charCodeAt(0)-65296;});" />
<input type="text" onfocus="this.style.imeMode='disabled'" />
<script language=javascript>
//全角空格为12288,半角空格为32
//其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
//document.write((65281).toString(16)+"----"+(65374).toString(16)+"----"+(12288).toString(16));
String.prototype.dbc2sbc = function ()
{
return this.replace(/[\uff01-\uff5e]/g,function(a){return String.fromCharCode(a.charCodeAt(0)-65248);}).replace(/\u3000/g," ");
}
document.write("ABC 123,我们都是好朋友".dbc2sbc());
</script>