最近项目需要一个功能,实现汉字转拼音功能,具体比如说输入一个“你好”,同时带出对应拼音“NiHao”,在此做一下记录
整体demo
<!doctype html> <html> <head> <meta charset="utf-8"> <title>如何实现汉字转拼音功能</title> </head> <script src="jquery.min.js"></script> <script src="Convert_Pinyin.js"></script> <script> var ConvertName = function(){ var chinaName = $('#chinaName').val(); //获取全写拼音(调用js中方法) var fullName = pinyin.getFullChars(chinaName); //获取简写拼音(调用js中方法) var easyName = pinyin.getCamelChars(chinaName); //给两个文本框赋值 $('#fullName').val(fullName); $('#easyName').val(easyName); } </script> <body> <div> 输入名称:<input type="text" id="chinaName" onBlur="ConvertName()" /> <br/> 全写拼音:<input type="text" id="fullName" /> <br/> 简写拼音:<input type="text" id="easyName" /> <br/> </div> </body> </html>
效果
汉字转拼音js下载路径:
链接: https://pan.baidu.com/s/1zAqTVcWBc4VKhUYmFLk8ww 提取码: u5nw