原生js实现全角与半角的相互转换

 

半角转全角

function ToDBC(txtstring) { 
  var tmp = ""; 
  for(var i=0;i<txtstring.length;i++{ 
    if(txtstring.charCodeAt(i)==32){ 
      tmp= tmp+ String.fromCharCode(12288); 
    } 
    if(txtstring.charCodeAt(i)<127){ 
      tmp=tmp+String.fromCharCode(txtstring.charCodeAt(i)+65248); 
    } 
  } 
  return tmp; 
}

 

全角转半角

function ToCDB(str) { 
  var tmp = ""; 
  for(var i=0;i<str.length;i++){ 
    if (str.charCodeAt(i) == 12288){
      tmp += String.fromCharCode(str.charCodeAt(i)-12256);
      continue;
    }
    if(str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375){ 
      tmp += String.fromCharCode(str.charCodeAt(i)-65248); 
    } 
    else{ 
      tmp += String.fromCharCode(str.charCodeAt(i)); 
    } 
  } 
  return tmp 
} 

 

来源:https://www.jb51.net/article/113972.htm


Fatal error: Uncaught Error: Call to undefined function create_function() in /www/wwwroot/www.menglei.net/wp-content/plugins/autoptimize/classes/autoptimizeStyles.php:96 Stack trace: #0 /www/wwwroot/www.menglei.net/wp-content/plugins/autoptimize/autoptimize.php(293): autoptimizeStyles->read() #1 [internal function]: autoptimize_end_buffering() #2 /www/wwwroot/www.menglei.net/wp-includes/functions.php(5349): ob_end_flush() #3 /www/wwwroot/www.menglei.net/wp-includes/class-wp-hook.php(310): wp_ob_end_flush_all() #4 /www/wwwroot/www.menglei.net/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters() #5 /www/wwwroot/www.menglei.net/wp-includes/plugin.php(517): WP_Hook->do_action() #6 /www/wwwroot/www.menglei.net/wp-includes/load.php(1252): do_action() #7 [internal function]: shutdown_action_hook() #8 {main} thrown in /www/wwwroot/www.menglei.net/wp-content/plugins/autoptimize/classes/autoptimizeStyles.php on line 96