知向前端
js 汉字获取首字母,汉字转拼音
2021-2-9 Jon

JS 地址


https://gitee.com/yinuocode/convert-pinyin



功能包括




getChineseStrPY 函数介绍



  1. 参数
    str 必须
    传入的字符串
    type 可选

    • 不传: 返回所有字符的首字母

    • irst: 返回第一个字符的首字母

    • all: 返回每一个字符的拼音全拼



  2. 返回值
    处理后的字符串



使用



  1. html 中引入 convert-pinyin.js

  2. getChineseStrPY('需要处理的字符串');



html test 代码



   html    54行

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>获取汉字首字母 js 插件使用</title>
</head>
<body>
<div>
<div>获取字符串第一个字符的首字母</div></div>
<input type="text" onblur="getInitial1(this.value)">
<button>获取首字母</button>
<div id="txt1"></div>
<div style="font-size: 12px; color: #999; margin-top: 20px;">多个字母开头的多音字会显示多个字母</div>
</div>
<div>
<div>获取字符串每个字符的首字母</div></div>
<input type="text" onblur="getInitial2(this.value)">
<button>获取首字母</button>
<div id="txt2"></div>
<div style="font-size: 12px; color: #999; margin-top: 20px;">多个字母开头的多音字会显示多个字母</div>
</div>
<div>
<div>获取字符串拼音</div></div>
<input type="text" onblur="getInitial3(this.value)">
<button>获取首字母</button>
<div id="txt3"></div>
<div style="font-size: 12px; color: #999; margin-top: 20px;">多个字母开头的多音字会显示多个字母</div>
</div>

<script src="./convert-pinyin.js"></script>
<script>
// 返回所有字符的首字母
function getInitial1(str) {
let py = getChineseStrPY(str);
console.log(py);
document.getElementById('txt1').textContent = py;
}
function getInitial2(str) {
// 返回第一个字符的首字母
let py = getChineseStrPY(str, 'first');
console.log(py);
document.getElementById('txt2').textContent = py;
}
// 返回每一个字符的拼音全拼
function getInitial3(str) {
let py = getChineseStrPY(str, 'all');
console.log(py);
document.getElementById('txt3').textContent = py;
}
</script>
</body>
</html>

评论:
TJEYOmVGhjkgQn
2023-12-16 08:07 回复
RwYfKjesAyik
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容