根据上一篇博客,继续是实现下一个功能,将汉字转化为拼音,要带声调。
需要用到的插件:https://download.csdn.net/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(当前汉字)这个方法即可。
这篇博客介绍如何将汉字转化为拼音并带有声调,主要依赖于特定的JavaScript插件,包括pinyin_dict_withtone.js和pinyinUtil.js。通过调用getPinyin方法,可以实现在输入汉字时自动转换成带声调的拼音。
1万+

被折叠的 条评论
为什么被折叠?



