LoginSignup
3
1

More than 5 years have passed since last update.

文字変換プラグイン

Last updated at Posted at 2017-11-06

簡単に文字変換

ここにあるプラグインをダウンロードする。

後はjqueryファイルを読み込み

簡単な使用方法

henkan.js

jQuery(function ($) {
            /** [全角英数字] => [半角英数字]**/
            var result = moji('test123').convert("ZEtoHE").toString() || '';
            //result => test123
});

この記述でinputのtype=textに入力された内容の全角英数は半角英数に変換される。
例:test123 => test123 
tesと123 => tesと123 //半角英数字のみ変換対象となり、その他の文字はなにもおこらない

converの変換内容
"ZEtoHE": 全角英数 => 半角英数
"ZStoHS": 全角スペース => 半角スペース
"HStoZS": 半角スペース => 全角スペースに
"HEtoZE": 半角英数 => 全角英数
"HGtoKK": ひらがな => カタカナ
"KKtoHG": カタカナ => ひらがな
"ZKtoHK": 全角カナ => 半角カナ
"HKtoZK": 半角カナ => 全角カナ

少し応用した例

henkan.js
//半角カタカナを全角カタカナに
//全角英数字を半角英数字に
jQuery(function ($) {
            /** 半角カタカナのみを全角カタカナに変換**/
            var kana = moji('テストてすとtest').convert("HKtoZK").toString() || '';
            //kana => テストてすとtest
            /** 全角英数字のみ半角英数字に変換**/
            var result = moji(kana).convert("ZEtoHE").toString() || '';
            //result => テストてすとtest
        }
});
//メソッドチェーンで繋いで、変換する
jQuery(function ($) {
            /** [半角カナ] => [全角カナ] => [ひらがな] **/
            moji("アイウエオ").convert("HKtoZK").convert("KKtoHG").toString();
            // => あいうえお
        }
});

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1