根据上一篇博客,继续是实现下一个功能,将汉字转化为拼音,要带声调。
需要用到的插件:/download/impossible1994727/12628685
根据项目需求引入自己需要的js,我这里用了pinyin_dict_withtone.js和pinyinUtil.js。
抽离出自己需要的方法如下:
function getPinyin(value) {// var value = document.getElementById('test').value;// var type = document.querySelector('[name="pinyin_type"]:checked').value;// var polyphone = document.querySelector('[name="polyphone"]').checked;var type = '0', polyphone = true;var result = '';if (value) {switch (type) {case '0':result = pinyinUtil.getPinyin(value, ' ', true, polyphone);break;case '1':result = pinyinUtil.getPinyin(value, ' ', false, polyphone);break;case '2':result = pinyinUtil.getFirstLetter(value, polyphone);break;default:break;}}$('.pinyin').text('拼音:'+result)}
每次更换汉字的时候执行getPinyin(当前汉字)这个方法即可。
如果觉得《鸡肋功能2————将汉字转化为拼音(带声调)》对你有帮助,请点赞、收藏,并留下你的观点哦!