功能説明
轉換成日文漢字,方便使用網頁或文書處理的預設日文漂亮字型
範例
textarea#old
textarea#newText
textarea
width: 300px
height: 400px
//展示用文字
var text="這是一段中文内容的説明文\n注意觀察【内】﹐【説】的變化";
//使用陣列紀錄
//持續更新中
var replaceArray=[
{from:"內",to: "内"},
{from:"值",to: "値"},
{from:"查",to: "査"},
{from:"啟",to: "啓"},
{from:"說",to: "説"},
{from:"產",to: "産"},
{from:"每",to: "毎"},
{from:"蔥",to: "葱"}
];
//沒有對應的日文漢字
//喵
//origText到時候代入old的值
var translate = function(origText){
for(i=0;i<replaceArray.length;i++){
//取代全部 參考這篇文章
var repAll=new RegExp(replaceArray[i].from, "g");
origText=origText.replace(repAll, replaceArray[i].to);
}
return origText;
}
var old=document.querySelector("#old");
var newText=document.querySelector("#newText");
//先把展示用文字帶入
old.textContent=text;
//建立翻譯功能
newText.textContent=translate(old.value);
//綁定當old有輸入事件,觸發翻譯
old.addEventListener("input",
function(){
newText.textContent=translate(old.value);
}
);
Display
See the Pen 漢字國字轉換器 by Ian Chen (@ianchen0419) on CodePen.